Date & Time

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.

parseToDate("2025-03-05") // returns Date object 

now(): Date

Gets the current time using Date.now.

now() // 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.

distance(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.

min(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.

max(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.

greaterThan(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.

lessThan(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.

between(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.

isPast(new Date("2020-01-01")) // returns true 

isFuture(date: Date): boolean

Returns true if the date is in the future.

isFuture(new Date("2030-01-01")) // returns true 

Date Mathematics

add(date: Date, parts: DateTime.PartsForMath): Date

Adds the specified duration to a date.

add(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.

subtract(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.).

startOf(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.

endOf(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.

format(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.

formatIso(new Date("2025-01-15")) // returns "2025-01-15T00:00:00.000Z" 

formatIsoDate(date: Date): string

Formats a date to a date string.

formatIsoDate(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.

getDay(new Date("2025-01-15")) // returns 15 

getMonth(date: Date): number

Gets the month from a Date object (1-12).

getMonth(new Date("2025-01-15")) // returns 1 

getYear(date: Date): number

Gets the year from a Date object.

getYear(new Date("2025-01-15")) // returns 2025 

getHours(date: Date): number

Gets the hours from a Date object.

getHours(new Date("2025-01-15T14:30:00")) // returns 14 

getMinutes(date: Date): number

Gets the minutes from a Date object.

getMinutes(new Date("2025-01-15T14:30:00")) // returns 30 

getSeconds(date: Date): number

Gets the seconds from a Date object.

getSeconds(new Date("2025-01-15T14:30:45")) // returns 45 

getMilliseconds(date: Date): number

Gets the milliseconds from a Date object.

getMilliseconds(new Date("2025-01-15T14:30:45.123")) // returns 123