isDate()function
Is a value a valid Date instance?
isDate(value: unknown): value is Date
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".getDateString() produces "2015-09-12", not an ISO UTC string.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).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")); // falseimport { 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"import { getTimestamp, requireTimestamp } from "shelving/util";
getTimestamp("2024-01-01"); // 1704067200000 (ms since epoch)
requireTimestamp("now"); // current time in msimport { 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 monthimport { getDay, DAYS } from "shelving/util";
getDay("2024-03-15"); // "Friday"
DAYS[0]; // "Sunday"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 nowIs a value a valid Date instance?
isDate(value: unknown): value is Date
Assert that a value is a valid Date instance.
assertDate(value: unknown, caller: AnyCaller = assertDate): asserts value is Date
Convert an unknown value to a valid Date instance, or return undefined if it couldn't be converted.
getDate(value: unknown): Date | undefined
Get a date representing this exact moment.
getNow(): Date
Get a date representing midnight of the previous day.
getYesterday(): Date
Get a date representing midnight of the current day.
getToday(): Date
Get a date representing midnight of the next day.
getTomorrow(): Date
Get a Date representing exactly midnight of the specified date.
getMidnight(target?: PossibleDate, caller: AnyCaller = getMidnight): Date
Get a Date representing midnight on Monday of the specified week.
getMonday(target?: PossibleDate, caller: AnyCaller = getMonday): Date
Get a Date representing midnight on the first day of the specified month.
getMonthStart(target?: PossibleDate, caller: AnyCaller = getMonthStart): Date
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
Convert an unknown value to a timestamp (milliseconds past Unix epoch), or undefined if it couldn't be converted.
getTimestamp(value?: unknown): number | undefined
Convert a possible date to a timestamp (milliseconds past Unix epoch), or throw RequiredError if it couldn't be converted.
requireTimestamp(value?: PossibleDate): number
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
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
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
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
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
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
Convert a Date instance to a day-of-week string like "Monday"
getDay(target?: PossibleDate): Day
Return a new date that increase or decreases the month based on an input date.
addYears(change: number, target?: PossibleDate, caller: AnyCaller = addYears): Date
Return a new date that increase or decreases the month based on an input date.
addMonths(change: number, target?: PossibleDate, caller: AnyCaller = addMonths): Date
Return a new date that increase or decreases the week based on an input date.
addWeeks(change: number, target?: PossibleDate, caller: AnyCaller = addWeeks): Date
Return a new date that increase or decreases the day based on an input date.
addDays(change: number, target?: PossibleDate, caller: AnyCaller = addDays): Date
Return a new date that increase or decreases the hour based on an input date.
addHours(change: number, target?: PossibleDate, caller: AnyCaller = addHours): Date
Return a new date that increase or decreases the minute based on an input date.
addMinutes(change: number, target?: PossibleDate, caller: AnyCaller = addMinutes): Date
Return a new date that increases or decreases the seconds based on an input date.
addSeconds(change: number, target?: PossibleDate, caller: AnyCaller = addSeconds): Date
Return a new date that increases or decreases the milliseconds based on an input date.
addMilliseconds(change: number, target?: PossibleDate, caller: AnyCaller = addMilliseconds): Date
Values that can be converted to dates.
"now" | "today" | "tomorrow" | "yesterday" | Date | number | string
Type listing day-of-week strings.
(typeof DAYS)[number]
List of day-of-week strings.