dateIsBetween

dateIsBetween

  • (date1: date, date2: date, opts?: Options) => (aDate: date) => boolean
  1. Test if a date is between two others. Inclusive by default.

    const today = new Date('2025-04-09')
    const lastWeek = new Date('2025-04-02')
    const isWithinTheLastWeek = dateIsBetween(lastWeek, today)
    
    const yesterday = new Date('2025-04-08')
    const tomorrow = new Date('2025-04-10')
    
    isWithinTheLastWeek(yesterday) // is true
    isWithinTheLastWeek(tomorrow) // is false
    const today = new Date('2025-04-09')
    const lastWeek = new Date('2025-04-02')
    const isWithinTheLastWeek = dateIsBetween(lastWeek, today)
    
    const yesterday = new Date('2025-04-08')
    const tomorrow = new Date('2025-04-10')
    
    isWithinTheLastWeek(yesterday) // is true
    isWithinTheLastWeek(tomorrow) // is false
    
    const today = new Date('2025-04-09')
    const lastWeek = new Date('2025-04-02')
    const isWithinTheLastWeek = dateIsBetween(
      lastWeek,
      today
    )
    
    const yesterday = new Date('2025-04-08')
    const tomorrow = new Date('2025-04-10')
    
    isWithinTheLastWeek(yesterday) // is true
    isWithinTheLastWeek(tomorrow) // is false
    const today = new Date('2025-04-09')
    const lastWeek = new Date('2025-04-02')
    const isWithinTheLastWeek = dateIsBetween(
      lastWeek,
      today
    )
    
    const yesterday = new Date('2025-04-08')
    const tomorrow = new Date('2025-04-10')
    
    isWithinTheLastWeek(yesterday) // is true
    isWithinTheLastWeek(tomorrow) // is false
    
  2. We often want to see if a date falls within a range. Below, we see if our registration is active. Note our use of exclusiveMax to ensure the registration deactivates at expiration.

    const registrationStart = new Date('2025-04-09')
    const expiration = new Date('2025-05-09')
    
    const isRegistrationActive = dateIsBetween(registrationStart, expiration, {
      exclusiveMax: true,
    })
    
    isRegistrationActive(registrationStart) // is true
    isRegistrationActive(expiration) // is false
    const registrationStart = new Date('2025-04-09')
    const expiration = new Date('2025-05-09')
    
    const isRegistrationActive = dateIsBetween(registrationStart, expiration, {
      exclusiveMax: true,
    })
    
    isRegistrationActive(registrationStart) // is true
    isRegistrationActive(expiration) // is false
    
    const registrationStart = new Date('2025-04-09')
    const expiration = new Date('2025-05-09')
    
    const isRegistrationActive = dateIsBetween(
      registrationStart,
      expiration,
      {
        exclusiveMax: true,
      }
    )
    
    isRegistrationActive(registrationStart) // is true
    isRegistrationActive(expiration) // is false
    const registrationStart = new Date('2025-04-09')
    const expiration = new Date('2025-05-09')
    
    const isRegistrationActive = dateIsBetween(
      registrationStart,
      expiration,
      {
        exclusiveMax: true,
      }
    )
    
    isRegistrationActive(registrationStart) // is true
    isRegistrationActive(expiration) // is false