shelving/util/datemodule

Parse, validate, format, and manipulate dates. The central idea is PossibleDate — a value that could be a date — which the helpers accept everywhere so you rarely need to call new Date() yourself.

  • getDate() accepts Date, number (ms since epoch), date/time strings, and the convenience strings "now", "today", "tomorrow", "yesterday".
  • All string output is local time, not UTC — getDateString() produces "2015-09-12", not an ISO UTC string.
  • Invalid Date objects (e.g. new Date("blah")) are treated the same as missing values — isDate() returns false, getDate returns undefined.
  • addMonths() and addYears() clamp to the end of the month to avoid JavaScript's rollover behaviour (e.g. adding one month to 31 August gives 30 September, not 1 October).

Usage

Parsing

ts
import { getDate, requireDate, isDate, assertDate } from "shelving/util";

getDate("now");          // current Date
getDate("today");        // midnight today
getDate("tomorrow");     // midnight tomorrow
getDate("2024-03-15");   // Date at that local date
getDate("18:30");        // today at 18:30
getDate(1710000000000);  // Date from Unix ms timestamp
getDate("not a date");   // undefined

requireDate("2024-03-15"); // Date or throws RequiredError
isDate(new Date("bad"));   // false

Formatting

ts
import { getDateString, getTimeString, getDateTimeString } from "shelving/util";
import { requireDateString, requireTimeString, requireDateTimeString } from "shelving/util";

getDateString("now");      // "2024-03-15"
getTimeString("now");      // "18:30:00"
getDateTimeString("now");  // "2024-03-15T18:30:00"

// Throwing variants — useful when you know the value is valid:
requireDateString("2024-01-01"); // "2024-01-01"

Timestamps

ts
import { getTimestamp, requireTimestamp } from "shelving/util";

getTimestamp("2024-01-01"); // 1704067200000 (ms since epoch)
requireTimestamp("now");    // current time in ms

Anchor dates

ts
import { getNow, getToday, getTomorrow, getYesterday, getMidnight, getMonday, getMonthStart } from "shelving/util";

getNow();              // current moment
getToday();            // midnight today
getMidnight("2024-03-15"); // midnight on that date
getMonday();           // midnight on Monday of current week
getMonthStart();       // midnight on the 1st of current month

Day of week

ts
import { getDay, DAYS } from "shelving/util";

getDay("2024-03-15"); // "Friday"
DAYS[0];              // "Sunday"

Date arithmetic

ts
import { addDays, addWeeks, addMonths, addYears, addHours, addMinutes, addSeconds, addMilliseconds } from "shelving/util";

addDays(7, "2024-03-01");    // 2024-03-08
addMonths(1, "2024-01-31");  // 2024-02-29 (leap year) or 2024-02-28
addYears(-1, "2024-02-29");  // 2023-02-28 (clamped, not a leap year)
addHours(2, "now");          // 2 hours from now

Functions

Go

isDate()function

Is a value a valid Date instance?

isDate(value: unknown): value is Date
Go

assertDate()function

Assert that a value is a valid Date instance.

assertDate(value: unknown, caller: AnyCaller = assertDate): asserts value is Date
Go

getDate()function

Convert an unknown value to a valid Date instance, or return undefined if it couldn't be converted.

getDate(value: unknown): Date | undefined
Go

getNow()function

Get a date representing this exact moment.

getNow(): Date
Go

getYesterday()function

Get a date representing midnight of the previous day.

getYesterday(): Date
Go

getToday()function

Get a date representing midnight of the current day.

getToday(): Date
Go

getTomorrow()function

Get a date representing midnight of the next day.

getTomorrow(): Date
Go

getMidnight()function

Get a Date representing exactly midnight of the specified date.

getMidnight(target?: PossibleDate, caller: AnyCaller = getMidnight): Date
Go

getMonday()function

Get a Date representing midnight on Monday of the specified week.

getMonday(target?: PossibleDate, caller: AnyCaller = getMonday): Date
Go

getMonthStart()function

Get a Date representing midnight on the first day of the specified month.

getMonthStart(target?: PossibleDate, caller: AnyCaller = getMonthStart): Date
Go

requireDate()function

Convert a possible date to a Date instance, or throw RequiredError if it couldn't be converted.

requireDate(value: PossibleDate = "now", caller: AnyCaller = requireDate): Date
Go

getTimestamp()function

Convert an unknown value to a timestamp (milliseconds past Unix epoch), or undefined if it couldn't be converted.

getTimestamp(value?: unknown): number | undefined
Go

requireTimestamp()function

Convert a possible date to a timestamp (milliseconds past Unix epoch), or throw RequiredError if it couldn't be converted.

requireTimestamp(value?: PossibleDate): number
Go

getDateTimeString()function

Convert an unknown value to a local date string like "2015-09-12T18:30:00", or undefined if it couldn't be converted.

getDateTimeString(value?: unknown): string | undefined
Go

requireDateTimeString()function

Convert a possible Date instance to a local YMD string like "2015-09-12T18:30:00", or throw RequiredError if it couldn't be converted.

requireDateTimeString(value?: PossibleDate, caller: AnyCaller = requireDateTimeString): string
Go

getDateString()function

Convert an unknown value to a local date string like "2015-09-12", or undefined if it couldn't be converted.

getDateString(value?: unknown): string | undefined
Go

requireDateString()function

Convert a possible Date instance to a local date string like "2015-09-12", or throw RequiredError if it couldn't be converted.

requireDateString(value?: PossibleDate, caller: AnyCaller = requireDateString): string
Go

getTimeString()function

Convert an unknown value to a local time string like "18:32:00", or undefined if it couldn't be converted.

getTimeString(value?: unknown): string | undefined
Go

requireTimeString()function

Convert a possible Date instance to local time string like "18:32:00", or throw RequiredError if it couldn't be converted.

requireTimeString(value?: PossibleDate, caller: AnyCaller = requireTimeString): string
Go

getDay()function

Convert a Date instance to a day-of-week string like "Monday"

getDay(target?: PossibleDate): Day
Go

addYears()function

Return a new date that increase or decreases the month based on an input date.

addYears(change: number, target?: PossibleDate, caller: AnyCaller = addYears): Date
Go

addMonths()function

Return a new date that increase or decreases the month based on an input date.

addMonths(change: number, target?: PossibleDate, caller: AnyCaller = addMonths): Date
Go

addWeeks()function

Return a new date that increase or decreases the week based on an input date.

addWeeks(change: number, target?: PossibleDate, caller: AnyCaller = addWeeks): Date
Go

addDays()function

Return a new date that increase or decreases the day based on an input date.

addDays(change: number, target?: PossibleDate, caller: AnyCaller = addDays): Date
Go

addHours()function

Return a new date that increase or decreases the hour based on an input date.

addHours(change: number, target?: PossibleDate, caller: AnyCaller = addHours): Date
Go

addMinutes()function

Return a new date that increase or decreases the minute based on an input date.

addMinutes(change: number, target?: PossibleDate, caller: AnyCaller = addMinutes): Date
Go

addSeconds()function

Return a new date that increases or decreases the seconds based on an input date.

addSeconds(change: number, target?: PossibleDate, caller: AnyCaller = addSeconds): Date
Go

addMilliseconds()function

Return a new date that increases or decreases the milliseconds based on an input date.

addMilliseconds(change: number, target?: PossibleDate, caller: AnyCaller = addMilliseconds): Date

Types

Go

PossibleDatetype

Values that can be converted to dates.

"now" | "today" | "tomorrow" | "yesterday" | Date | number | string
Go

Daytype

Type listing day-of-week strings.

(typeof DAYS)[number]

Constants

Go

DAYSconstant

List of day-of-week strings.