@algosail/date
v0.1.0
Published
Small collection of FP utilities for working with date values.
Readme
@algosail/date
Date comparison and parsing utilities. All comparisons are based on milliseconds since epoch (valueOf()).
Contents
equals
equals :: Date -> Date -> BooleanTrue when both dates represent the same instant.
equals(new Date(0))(new Date(0)) // => true
equals(new Date(0))(new Date(1)) // => false
const d = new Date('2024-01-01')
equals(d)(new Date('2024-01-01')) // => true
equals(d)(new Date('2024-01-02')) // => falselte / lt / gte / gt
lte :: Date -> Date -> Boolean
lt :: Date -> Date -> Boolean
gte :: Date -> Date -> Boolean
gt :: Date -> Date -> BooleanTemporal comparisons.
const d1 = new Date('2024-01-01')
const d2 = new Date('2024-06-01')
lte(d1)(d2) // => true (d1 is before or equal to d2)
lte(d2)(d1) // => false
lt(d1)(d2) // => true
lt(d1)(d1) // => false (not strictly before itself)
gte(d2)(d1) // => true
gt(d2)(d1) // => true
gt(d1)(d1) // => falsemin / max / clamp
min :: Date -> Date -> Date
max :: Date -> Date -> Date
clamp :: Date -> Date -> Date -> Dateconst jan = new Date('2024-01-01')
const jun = new Date('2024-06-01')
const dec = new Date('2024-12-01')
min(jan)(jun) // => jan
max(jan)(jun) // => jun
clamp(jan)(jun)(dec) // => jun (dec is after hi)
clamp(jan)(jun)(jan) // => jan (on lo boundary)
clamp(jan)(dec)(new Date('2024-03-15')) // => new Date('2024-03-15') (in range)parseDate
parseDate :: String -> Maybe DateParses a date string — Just(Date) on success, Nothing for invalid input.
parseDate('2020-01-01') // => just(new Date('2020-01-01'))
parseDate('2024-12-31T23:59:59') // => just(Date)
parseDate('not a date') // => nothing()
parseDate('') // => nothing()