isBefore

isBefore

  • (date1: date) => (date2: date) => boolean
  1. Test if a date is before another

    const janFirst2025 = new Date('2025 1 1')
    const isBeforeJanFirst2025 = isBefore(janFirst2025)
    
    const decFirst2024 = new Date('2024 12 1')
    isBeforeJanFirst2025(decFirst2024) // is true
    
    const now = new Date()
    isBeforeJanFirst2025(now) // is false
    const janFirst2025 = new Date('2025 1 1')
    const isBeforeJanFirst2025 = isBefore(janFirst2025)
    
    const decFirst2024 = new Date('2024 12 1')
    isBeforeJanFirst2025(decFirst2024) // is true
    
    const now = new Date()
    isBeforeJanFirst2025(now) // is false
    
    const janFirst2025 = new Date('2025 1 1')
    const isBeforeJanFirst2025 =
      isBefore(janFirst2025)
    
    const decFirst2024 = new Date('2024 12 1')
    isBeforeJanFirst2025(decFirst2024) // is true
    
    const now = new Date()
    isBeforeJanFirst2025(now) // is false
    const janFirst2025 = new Date('2025 1 1')
    const isBeforeJanFirst2025 =
      isBefore(janFirst2025)
    
    const decFirst2024 = new Date('2024 12 1')
    isBeforeJanFirst2025(decFirst2024) // is true
    
    const now = new Date()
    isBeforeJanFirst2025(now) // is false
    
  2. Sometimes we need to compare dates. Below, we have some car rental applicants. Unfortunately, you have to be at least 25 years old to rent from our company. Let's only process those born before the cutoff date.

    // assume today is 2025 1 15
    const cutoffDate = new Date('2000 1 16')
    
    const applicants = {
      kim: {
        dateOfBirth: new Date('2002 6 8'),
      },
      grace: {
        dateOfBirth: new Date('1999 12 15'),
      },
    }
    
    const canRent = compose([get('dateOfBirth'), isBefore(cutoffDate)])
    const keepValid = keepWhen(canRent)
    const validApplicants = keepValid(applicants)
    
    console.log(validApplicants)
    // is {
    //   grace: {
    //     dateOfBirth: 1999 12 15
    //   }
    // }
    // assume today is 2025 1 15
    const cutoffDate = new Date('2000 1 16')
    
    type Applicant = { dateOfBirth: Date }
    type Applicants = Record<string, Applicant>
    const applicants: Applicants = {
      kim: {
        dateOfBirth: new Date('2002 6 8'),
      },
      grace: {
        dateOfBirth: new Date('1999 12 15'),
      },
    }
    
    const canRent = compose([get('dateOfBirth')<Applicant>, isBefore(cutoffDate)])
    const keepValid = keepWhen(canRent)<Applicants>
    const validApplicants = keepValid(applicants)
    
    console.log(validApplicants)
    // is {
    //   grace: {
    //     dateOfBirth: 1999 12 15
    //   }
    // }
    
    // assume today is 2025 1 15
    const cutoffDate = new Date('2000 1 16')
    
    const applicants = {
      kim: {
        dateOfBirth: new Date('2002 6 8'),
      },
      grace: {
        dateOfBirth: new Date('1999 12 15'),
      },
    }
    
    const canRent = compose([
      get('dateOfBirth'),
      isBefore(cutoffDate),
    ])
    const keepValid = keepWhen(canRent)
    const validApplicants = keepValid(applicants)
    
    console.log(validApplicants)
    // is {
    //   grace: {
    //     dateOfBirth: 1999 12 15
    //   }
    // }
    // assume today is 2025 1 15
    const cutoffDate = new Date('2000 1 16')
    
    type Applicant = { dateOfBirth: Date }
    type Applicants = Record<string, Applicant>
    const applicants: Applicants = {
      kim: {
        dateOfBirth: new Date('2002 6 8'),
      },
      grace: {
        dateOfBirth: new Date('1999 12 15'),
      },
    }
    
    const canRent = compose([
      get('dateOfBirth')<Applicant>,
      isBefore(cutoffDate),
    ])
    const keepValid = keepWhen(canRent)<Applicants>
    const validApplicants = keepValid(applicants)
    
    console.log(validApplicants)
    // is {
    //   grace: {
    //     dateOfBirth: 1999 12 15
    //   }
    // }