This module provides a collection of utility functions for working with dates and times. It extends functionality from the Effect-TS library.
Basic Operations
parseToDate(date: string): Date
Parses a date string to a Date object.
typescriptparseToDate("2025-03-05") // returns Date object
now(): Date
Gets the current time using Date.now.
typescriptnow() // returns current Date
Date Comparisons
distance(self: Date, other: Date): number
Calculates the difference between two dates in milliseconds. Returns positive if other is after self.
typescriptdistance(new Date("2025-01-01"), new Date("2025-01-02")) // returns 86400000 (1 day in ms)
min(self: Date, other: Date): Date
Returns the earlier of two dates.
typescriptmin(new Date("2025-01-01"), new Date("2025-01-02")) // returns Date("2025-01-01")
max(self: Date, other: Date): Date
Returns the later of two dates.
typescriptmax(new Date("2025-01-01"), new Date("2025-01-02")) // returns Date("2025-01-02")
greaterThan(self: Date, other: Date): boolean
Returns true if the first date is after the second.
typescriptgreaterThan(new Date("2025-01-02"), new Date("2025-01-01")) // returns true
lessThan(self: Date, other: Date): boolean
Returns true if the first date is before the second.
typescriptlessThan(new Date("2025-01-01"), new Date("2025-01-02")) // returns true
between(self: Date, minimum: Date, maximum: Date): boolean
Returns true if the date is between the minimum and maximum dates.
typescriptbetween(new Date("2025-01-02"), new Date("2025-01-01"), new Date("2025-01-03")) // returns true
isPast(date: Date): boolean
Returns true if the date is in the past.
typescriptisPast(new Date("2020-01-01")) // returns true
isFuture(date: Date): boolean
Returns true if the date is in the future.
typescriptisFuture(new Date("2030-01-01")) // returns true
Date Mathematics
add(date: Date, parts: DateTime.PartsForMath): Date
Adds the specified duration to a date.
typescriptadd(new Date("2025-01-01"), { days: 1 }) // returns Date("2025-01-02")
subtract(date: Date, parts: DateTime.PartsForMath): Date
Subtracts the specified duration from a date.
typescriptsubtract(new Date("2025-01-02"), { days: 1 }) // returns Date("2025-01-01")
startOf(date: Date, unit: DateTime.UnitSingular, weekStartsOn?: 0-6): Date
Converts a date to the start of the given unit (year, month, week, day, etc.).
typescriptstartOf(new Date("2025-01-15"), "month") // returns Date("2025-01-01")
endOf(date: Date, unit: DateTime.UnitSingular, weekStartsOn?: 0-6): Date
Converts a date to the end of the given unit.
typescriptendOf(new Date("2025-01-15"), "month") // returns Date("2025-01-31T23:59:59.999")
Date Formatting
format(date: Date, options: DateTimeFormatOptions): string
Formats a date to a string using the given options.
typescriptformat(new Date("2025-01-15"), { year: 'numeric', month: 'long', day: 'numeric' }) // returns "January 15, 2025"
formatIso(date: Date): string
Formats a date to an ISO string.
typescriptformatIso(new Date("2025-01-15")) // returns "2025-01-15T00:00:00.000Z"
formatIsoDate(date: Date): string
Formats a date to an ISO date string.
typescriptformatIsoDate(new Date("2025-01-15")) // returns "2025-01-15"
Date Components
getDay(date: Date): number
Gets the day of the month from a Date object.
typescriptgetDay(new Date("2025-01-15")) // returns 15
getMonth(date: Date): number
Gets the month from a Date object (1-12).
typescriptgetMonth(new Date("2025-01-15")) // returns 1
getYear(date: Date): number
Gets the year from a Date object.
typescriptgetYear(new Date("2025-01-15")) // returns 2025
getHours(date: Date): number
Gets the hours from a Date object.
typescriptgetHours(new Date("2025-01-15T14:30:00")) // returns 14
getMinutes(date: Date): number
Gets the minutes from a Date object.
typescriptgetMinutes(new Date("2025-01-15T14:30:00")) // returns 30
getSeconds(date: Date): number
Gets the seconds from a Date object.
typescriptgetSeconds(new Date("2025-01-15T14:30:45")) // returns 45
getMilliseconds(date: Date): number
Gets the milliseconds from a Date object.
typescriptgetMilliseconds(new Date("2025-01-15T14:30:45.123")) // returns 123