Validation And Boundaries

Use these helpers when app code needs to accept external input, guard form values, or build calendar-style boundaries before doing date math.

Validate before parsing

validateTimestamp() is a lightweight grammar check. Use parseTimestamp() when you need a full immutable object.

import { function parseTimestamp(input: string, now?: Timestamp | null): Timestamp | null
Converts a supported date or date-time string into a formatted Timestamp object. If `now` is supplied, the returned timestamp also includes relative flags such as `past`, `current`, `future`, and `currentWeekday`.
@paraminput Date or date-time string, such as `YYYY-MM-DD`, `YYYY-MM-DD HH:mm:ss`, or an ISO-like value with optional milliseconds and timezone suffix.@paramnow Optional Timestamp used to calculate relative flags.@returnsFormatted Timestamp object, or `null` when the input cannot be parsed.@categoryparsing
parseTimestamp
, function validateTimestamp(input: string): boolean
Validates whether an input string matches the supported timestamp grammar.
@paraminput A string in the form `YYYY-MM-DD`, `YYYY-MM-DD HH:mm`, or a full ISO-like date time.@returnsTrue if parseable@categoryvalidation
validateTimestamp
} from '@timestamp-js/core'
function validateTimestamp(input: string): boolean
Validates whether an input string matches the supported timestamp grammar.
@paraminput A string in the form `YYYY-MM-DD`, `YYYY-MM-DD HH:mm`, or a full ISO-like date time.@returnsTrue if parseable@categoryvalidation
validateTimestamp
('2036-06-08T09:30:15.250Z') // true
function validateTimestamp(input: string): boolean
Validates whether an input string matches the supported timestamp grammar.
@paraminput A string in the form `YYYY-MM-DD`, `YYYY-MM-DD HH:mm`, or a full ISO-like date time.@returnsTrue if parseable@categoryvalidation
validateTimestamp
('not a date') // false
const const timestamp: Timestamp | nulltimestamp = function validateTimestamp(input: string): boolean
Validates whether an input string matches the supported timestamp grammar.
@paraminput A string in the form `YYYY-MM-DD`, `YYYY-MM-DD HH:mm`, or a full ISO-like date time.@returnsTrue if parseable@categoryvalidation
validateTimestamp
('2036-06-08') ? function parseTimestamp(input: string, now?: Timestamp | null): Timestamp | null
Converts a supported date or date-time string into a formatted Timestamp object. If `now` is supplied, the returned timestamp also includes relative flags such as `past`, `current`, `future`, and `currentWeekday`.
@paraminput Date or date-time string, such as `YYYY-MM-DD`, `YYYY-MM-DD HH:mm:ss`, or an ISO-like value with optional milliseconds and timezone suffix.@paramnow Optional Timestamp used to calculate relative flags.@returnsFormatted Timestamp object, or `null` when the input cannot be parsed.@categoryparsing
parseTimestamp
('2036-06-08') : null
const timestamp: Timestamp | nulltimestamp?.Timestamp.date: string | undefined
Date string in `YYYY-MM-DD` form when the timestamp has a day.
date
// "2036-06-08"

Use the low-level parser when you only need fields

parsed() is intentionally smaller than parseTimestamp(). It reads fields and preserves timezone suffixes, but does not fill derived values such as weekday, day of year, or workweek.

import { function parsed(input: string): Timestamp | null
Fast low-level parser for date and date-time strings. This parser fills numeric fields, but does not update formatted date, weekday, day-of-year, workweek, or relative flags. Use parseTimestamp() when those derived fields are needed.
@paraminput In the form `YYYY-MM-DD`, `YYYY-MM-DD HH:mm:ss`, or an ISO-like date time with optional milliseconds and timezone suffix.@returnsMinimal Timestamp object, or `null` when the input cannot be parsed.@categoryparsing
parsed
, function parseTimestamp(input: string, now?: Timestamp | null): Timestamp | null
Converts a supported date or date-time string into a formatted Timestamp object. If `now` is supplied, the returned timestamp also includes relative flags such as `past`, `current`, `future`, and `currentWeekday`.
@paraminput Date or date-time string, such as `YYYY-MM-DD`, `YYYY-MM-DD HH:mm:ss`, or an ISO-like value with optional milliseconds and timezone suffix.@paramnow Optional Timestamp used to calculate relative flags.@returnsFormatted Timestamp object, or `null` when the input cannot be parsed.@categoryparsing
parseTimestamp
} from '@timestamp-js/core'
const const minimal: Timestampminimal = function parsed(input: string): Timestamp | null
Fast low-level parser for date and date-time strings. This parser fills numeric fields, but does not update formatted date, weekday, day-of-year, workweek, or relative flags. Use parseTimestamp() when those derived fields are needed.
@paraminput In the form `YYYY-MM-DD`, `YYYY-MM-DD HH:mm:ss`, or an ISO-like date time with optional milliseconds and timezone suffix.@returnsMinimal Timestamp object, or `null` when the input cannot be parsed.@categoryparsing
parsed
('2036-06-08T09:30:15-07:00')!
const const formatted: Timestampformatted = function parseTimestamp(input: string, now?: Timestamp | null): Timestamp | null
Converts a supported date or date-time string into a formatted Timestamp object. If `now` is supplied, the returned timestamp also includes relative flags such as `past`, `current`, `future`, and `currentWeekday`.
@paraminput Date or date-time string, such as `YYYY-MM-DD`, `YYYY-MM-DD HH:mm:ss`, or an ISO-like value with optional milliseconds and timezone suffix.@paramnow Optional Timestamp used to calculate relative flags.@returnsFormatted Timestamp object, or `null` when the input cannot be parsed.@categoryparsing
parseTimestamp
('2036-06-08T09:30:15-07:00')!
const minimal: Timestampminimal.Timestamp.timezone?: string | undefined
Optional parsed ISO timezone suffix such as `Z`, `+06:00`, or `-0700`. The suffix is preserved for callers, but parsing does not convert the wall-clock values into another timezone.
timezone
// "-07:00"
const minimal: Timestampminimal.Timestamp.doy?: number | undefined
Day of the year.
doy
// 0
const formatted: Timestampformatted.Timestamp.doy?: number | undefined
Day of the year.
doy
// 160

Validate numeric input

validateNumber() is useful before converting text input into offsets, intervals, or form-driven date math.

import { function validateNumber(input: string | number): boolean
Validates if the input is a finite number.
@paraminput - The value to be validated. Can be a string or a number.@returnsA boolean indicating whether the input is a finite number. Returns true if the input is a finite number, false otherwise.@categoryvalidation
validateNumber
} from '@timestamp-js/core'
function validateNumber(input: string | number): boolean
Validates if the input is a finite number.
@paraminput - The value to be validated. Can be a string or a number.@returnsA boolean indicating whether the input is a finite number. Returns true if the input is a finite number, false otherwise.@categoryvalidation
validateNumber
('15') // true
function validateNumber(input: string | number): boolean
Validates if the input is a finite number.
@paraminput - The value to be validated. Can be a string or a number.@returnsA boolean indicating whether the input is a finite number. Returns true if the input is a finite number, false otherwise.@categoryvalidation
validateNumber
(30) // true
function validateNumber(input: string | number): boolean
Validates if the input is a finite number.
@paraminput - The value to be validated. Can be a string or a number.@returnsA boolean indicating whether the input is a finite number. Returns true if the input is a finite number, false otherwise.@categoryvalidation
validateNumber
('fifteen') // false

Inspect Gregorian month facts

Use isLeapYear() and daysInMonth() when you need to explain or constrain calendar choices.

import { function daysInMonth(year: number, month: number, calendar?: CalendarSystem): number
Returns the days of the specified month in a year
@paramyear The year (ie: 1999, 2020)@parammonth The month number in the selected calendar, where January is `1` for Gregorian.@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsThe number of days in the month (corrected for leap years)@categorycalendar
daysInMonth
, function isLeapYear(year: number, calendar?: CalendarSystem): boolean
Returns if the passed year is a leap year
@paramyear The year to check (ie: 1999, 2020)@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsTrue if the year is a leap year@categorycalendar
isLeapYear
} from '@timestamp-js/core'
function isLeapYear(year: number, calendar?: CalendarSystem): boolean
Returns if the passed year is a leap year
@paramyear The year to check (ie: 1999, 2020)@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsTrue if the year is a leap year@categorycalendar
isLeapYear
(2036) // true
function daysInMonth(year: number, month: number, calendar?: CalendarSystem): number
Returns the days of the specified month in a year
@paramyear The year (ie: 1999, 2020)@parammonth The month number in the selected calendar, where January is `1` for Gregorian.@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsThe number of days in the month (corrected for leap years)@categorycalendar
daysInMonth
(2036, 2) // 29
function daysInMonth(year: number, month: number, calendar?: CalendarSystem): number
Returns the days of the specified month in a year
@paramyear The year (ie: 1999, 2020)@parammonth The month number in the selected calendar, where January is `1` for Gregorian.@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsThe number of days in the month (corrected for leap years)@categorycalendar
daysInMonth
(2037, 2) // 28

Format small numeric pieces

padNumber() is intentionally simple, but it keeps custom output consistent with Timestamp’s own formatting.

import { function padNumber(x: number, length: number): string
Pads a number to a requested string length. Useful for formatting values such as `5` as `05`.
@paramx The number to pad@paramlength The length of the required number as a string@returnsThe padded number (as a string). (ie: 5 = '05')@categoryformatting
padNumber
} from '@timestamp-js/core'
function padNumber(x: number, length: number): string
Pads a number to a requested string length. Useful for formatting values such as `5` as `05`.
@paramx The number to pad@paramlength The length of the required number as a string@returnsThe padded number (as a string). (ie: 5 = '05')@categoryformatting
padNumber
(7, 2) // "07"
function padNumber(x: number, length: number): string
Pads a number to a requested string length. Useful for formatting values such as `5` as `05`.
@paramx The number to pad@paramlength The length of the required number as a string@returnsThe padded number (as a string). (ie: 5 = '05')@categoryformatting
padNumber
(42, 4) // "0042"

Read period boundaries

Start/end helpers are useful for month grids, week views, and reporting ranges.

import {
  function getDate(timestamp: Timestamp): string
Formats the date portion of a Timestamp object.
@paramtimestamp Timestamp object to format.@returnsDate string such as `YYYY-MM-DD`.@categoryconversion
getDate
,
function getEndOfDay(timestamp: Timestamp): Timestamp
Returns a Timestamp at the end of the same calendar day.
@paramtimestamp Timestamp object to transform.@returnsNew Timestamp at `23:59:59.999`.@categoryranges
getEndOfDay
,
function getEndOfMonth(timestamp: Timestamp, calendar?: CalendarSystem): Timestamp
Finds the end of the month based on the passed in Timestamp
@paramtimestamp The Timestamp to use to find the end of the month@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsA Timestamp of the end of the month@categoryranges
getEndOfMonth
,
function getEndOfWeek(timestamp: Timestamp, weekdays: number[], today: Timestamp, calendar?: CalendarSystem): Timestamp
Returns the end of the week for a Timestamp and weekday set. If a current Timestamp is provided, the returned Timestamp includes updated relative information.
@paramtimestamp The Timestamp to use to find the end of the week@paramweekdays The array is [0,1,2,3,4,5,6] where 0=Sunday and 6=Saturday@paramtoday Current timestamp used to update relative information@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsA new Timestamp representing the end of the week@categoryranges
getEndOfWeek
,
function getEndOfYear(timestamp: Timestamp, calendar?: CalendarSystem): Timestamp
Returns a Timestamp at the end of the same calendar year.
@paramtimestamp Timestamp object to transform.@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsNew Timestamp for the last calendar day of the year at `23:59:59.999`.@categoryranges
getEndOfYear
,
function getStartOfDay(timestamp: Timestamp): Timestamp
Returns a Timestamp at the start of the same calendar day.
@paramtimestamp Timestamp object to transform.@returnsNew Timestamp at `00:00`.@categoryranges
getStartOfDay
,
function getStartOfMonth(timestamp: Timestamp, calendar?: CalendarSystem): Timestamp
Finds the start of the month based on the passed in Timestamp
@paramtimestamp The Timestamp to use to find the start of the month@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsA Timestamp of the start of the month@categoryranges
getStartOfMonth
,
function getStartOfWeek(timestamp: Timestamp, weekdays: number[], today: Timestamp, calendar?: CalendarSystem): Timestamp
Returns the start of the week for a Timestamp and weekday set. If a current Timestamp is provided, the returned Timestamp includes updated relative information.
@paramtimestamp The Timestamp to use to find the start of the week@paramweekdays The array is [0,1,2,3,4,5,6] where 0=Sunday and 6=Saturday@paramtoday Current timestamp used to update relative information@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsA new Timestamp representing the start of the week@categoryranges
getStartOfWeek
,
function getStartOfYear(timestamp: Timestamp, calendar?: CalendarSystem): Timestamp
Returns a Timestamp at the start of the same calendar year.
@paramtimestamp Timestamp object to transform.@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsNew Timestamp for the first calendar day of the year at `00:00`.@categoryranges
getStartOfYear
,
function parseTimestamp(input: string, now?: Timestamp | null): Timestamp | null
Converts a supported date or date-time string into a formatted Timestamp object. If `now` is supplied, the returned timestamp also includes relative flags such as `past`, `current`, `future`, and `currentWeekday`.
@paraminput Date or date-time string, such as `YYYY-MM-DD`, `YYYY-MM-DD HH:mm:ss`, or an ISO-like value with optional milliseconds and timezone suffix.@paramnow Optional Timestamp used to calculate relative flags.@returnsFormatted Timestamp object, or `null` when the input cannot be parsed.@categoryparsing
parseTimestamp
,
} from '@timestamp-js/core' const const now: Timestampnow = function parseTimestamp(input: string, now?: Timestamp | null): Timestamp | null
Converts a supported date or date-time string into a formatted Timestamp object. If `now` is supplied, the returned timestamp also includes relative flags such as `past`, `current`, `future`, and `currentWeekday`.
@paraminput Date or date-time string, such as `YYYY-MM-DD`, `YYYY-MM-DD HH:mm:ss`, or an ISO-like value with optional milliseconds and timezone suffix.@paramnow Optional Timestamp used to calculate relative flags.@returnsFormatted Timestamp object, or `null` when the input cannot be parsed.@categoryparsing
parseTimestamp
('2036-06-08')!
const const weekdays: number[]weekdays = [0, 1, 2, 3, 4, 5, 6] function getStartOfDay(timestamp: Timestamp): Timestamp
Returns a Timestamp at the start of the same calendar day.
@paramtimestamp Timestamp object to transform.@returnsNew Timestamp at `00:00`.@categoryranges
getStartOfDay
(const now: Timestampnow).Timestamp.time?: string | undefined
Formatted time string. Minute precision is formatted as `HH:mm`; second precision as `HH:mm:ss`; millisecond precision as `HH:mm:ss.SSS`.
time
// "00:00"
function getEndOfDay(timestamp: Timestamp): Timestamp
Returns a Timestamp at the end of the same calendar day.
@paramtimestamp Timestamp object to transform.@returnsNew Timestamp at `23:59:59.999`.@categoryranges
getEndOfDay
(const now: Timestampnow).Timestamp.time?: string | undefined
Formatted time string. Minute precision is formatted as `HH:mm`; second precision as `HH:mm:ss`; millisecond precision as `HH:mm:ss.SSS`.
time
// "23:59:59.999"
function getDate(timestamp: Timestamp): string
Formats the date portion of a Timestamp object.
@paramtimestamp Timestamp object to format.@returnsDate string such as `YYYY-MM-DD`.@categoryconversion
getDate
(function getStartOfWeek(timestamp: Timestamp, weekdays: number[], today: Timestamp, calendar?: CalendarSystem): Timestamp
Returns the start of the week for a Timestamp and weekday set. If a current Timestamp is provided, the returned Timestamp includes updated relative information.
@paramtimestamp The Timestamp to use to find the start of the week@paramweekdays The array is [0,1,2,3,4,5,6] where 0=Sunday and 6=Saturday@paramtoday Current timestamp used to update relative information@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsA new Timestamp representing the start of the week@categoryranges
getStartOfWeek
(const now: Timestampnow, const weekdays: number[]weekdays, const now: Timestampnow)) // "2036-06-08"
function getDate(timestamp: Timestamp): string
Formats the date portion of a Timestamp object.
@paramtimestamp Timestamp object to format.@returnsDate string such as `YYYY-MM-DD`.@categoryconversion
getDate
(function getEndOfWeek(timestamp: Timestamp, weekdays: number[], today: Timestamp, calendar?: CalendarSystem): Timestamp
Returns the end of the week for a Timestamp and weekday set. If a current Timestamp is provided, the returned Timestamp includes updated relative information.
@paramtimestamp The Timestamp to use to find the end of the week@paramweekdays The array is [0,1,2,3,4,5,6] where 0=Sunday and 6=Saturday@paramtoday Current timestamp used to update relative information@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsA new Timestamp representing the end of the week@categoryranges
getEndOfWeek
(const now: Timestampnow, const weekdays: number[]weekdays, const now: Timestampnow)) // "2036-06-14"
function getDate(timestamp: Timestamp): string
Formats the date portion of a Timestamp object.
@paramtimestamp Timestamp object to format.@returnsDate string such as `YYYY-MM-DD`.@categoryconversion
getDate
(function getStartOfMonth(timestamp: Timestamp, calendar?: CalendarSystem): Timestamp
Finds the start of the month based on the passed in Timestamp
@paramtimestamp The Timestamp to use to find the start of the month@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsA Timestamp of the start of the month@categoryranges
getStartOfMonth
(const now: Timestampnow)) // "2036-06-01"
function getDate(timestamp: Timestamp): string
Formats the date portion of a Timestamp object.
@paramtimestamp Timestamp object to format.@returnsDate string such as `YYYY-MM-DD`.@categoryconversion
getDate
(function getEndOfMonth(timestamp: Timestamp, calendar?: CalendarSystem): Timestamp
Finds the end of the month based on the passed in Timestamp
@paramtimestamp The Timestamp to use to find the end of the month@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsA Timestamp of the end of the month@categoryranges
getEndOfMonth
(const now: Timestampnow)) // "2036-06-30"
function getDate(timestamp: Timestamp): string
Formats the date portion of a Timestamp object.
@paramtimestamp Timestamp object to format.@returnsDate string such as `YYYY-MM-DD`.@categoryconversion
getDate
(function getStartOfYear(timestamp: Timestamp, calendar?: CalendarSystem): Timestamp
Returns a Timestamp at the start of the same calendar year.
@paramtimestamp Timestamp object to transform.@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsNew Timestamp for the first calendar day of the year at `00:00`.@categoryranges
getStartOfYear
(const now: Timestampnow)) // "2036-01-01"
function getDate(timestamp: Timestamp): string
Formats the date portion of a Timestamp object.
@paramtimestamp Timestamp object to format.@returnsDate string such as `YYYY-MM-DD`.@categoryconversion
getDate
(function getEndOfYear(timestamp: Timestamp, calendar?: CalendarSystem): Timestamp
Returns a Timestamp at the end of the same calendar year.
@paramtimestamp Timestamp object to transform.@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsNew Timestamp for the last calendar day of the year at `23:59:59.999`.@categoryranges
getEndOfYear
(const now: Timestampnow)) // "2036-12-31"

Convert Unix epoch values

Unix helpers read and write Timestamp fields as UTC. Use them when storage or APIs use epoch seconds or milliseconds.

import {
  function fromUnixMilliseconds(milliseconds: number): Timestamp | null
Converts Unix milliseconds into an immutable Timestamp using UTC fields.
@parammilliseconds Unix milliseconds.@returnsTimestamp built from UTC fields, or `null` for invalid input.@categoryconversion
fromUnixMilliseconds
,
function fromUnixSeconds(seconds: number): Timestamp | null
Converts Unix seconds into an immutable Timestamp using UTC fields.
@paramseconds Unix seconds.@returnsTimestamp built from UTC fields, or `null` for invalid input.@categoryconversion
fromUnixSeconds
,
function getDateTime(timestamp: Timestamp): string
Formats a Timestamp as date plus time.
@paramtimestamp Timestamp object to format.@returnsDate-time string such as `YYYY-MM-DD HH:mm`.@categoryconversion
getDateTime
,
function parseTimestamp(input: string, now?: Timestamp | null): Timestamp | null
Converts a supported date or date-time string into a formatted Timestamp object. If `now` is supplied, the returned timestamp also includes relative flags such as `past`, `current`, `future`, and `currentWeekday`.
@paraminput Date or date-time string, such as `YYYY-MM-DD`, `YYYY-MM-DD HH:mm:ss`, or an ISO-like value with optional milliseconds and timezone suffix.@paramnow Optional Timestamp used to calculate relative flags.@returnsFormatted Timestamp object, or `null` when the input cannot be parsed.@categoryparsing
parseTimestamp
,
function toUnixMilliseconds(timestamp: Timestamp): number
Converts a Timestamp into Unix milliseconds by reading its fields as UTC. This is deterministic across server and client runtimes. It does not read or convert the optional `timezone` suffix stored on the Timestamp.
@paramtimestamp Timestamp object to convert.@returnsUnix milliseconds.@categoryconversion
toUnixMilliseconds
,
function toUnixSeconds(timestamp: Timestamp): number
Converts a Timestamp into Unix seconds by reading its fields as UTC. Milliseconds are floored because Unix seconds are integer-oriented.
@paramtimestamp Timestamp object to convert.@returnsUnix seconds.@categoryconversion
toUnixSeconds
,
} from '@timestamp-js/core' const const timestamp: Timestamptimestamp = function parseTimestamp(input: string, now?: Timestamp | null): Timestamp | null
Converts a supported date or date-time string into a formatted Timestamp object. If `now` is supplied, the returned timestamp also includes relative flags such as `past`, `current`, `future`, and `currentWeekday`.
@paraminput Date or date-time string, such as `YYYY-MM-DD`, `YYYY-MM-DD HH:mm:ss`, or an ISO-like value with optional milliseconds and timezone suffix.@paramnow Optional Timestamp used to calculate relative flags.@returnsFormatted Timestamp object, or `null` when the input cannot be parsed.@categoryparsing
parseTimestamp
('2036-06-08T09:30:15.250')!
const const milliseconds: numbermilliseconds = function toUnixMilliseconds(timestamp: Timestamp): number
Converts a Timestamp into Unix milliseconds by reading its fields as UTC. This is deterministic across server and client runtimes. It does not read or convert the optional `timezone` suffix stored on the Timestamp.
@paramtimestamp Timestamp object to convert.@returnsUnix milliseconds.@categoryconversion
toUnixMilliseconds
(const timestamp: Timestamptimestamp)
const const seconds: numberseconds = function toUnixSeconds(timestamp: Timestamp): number
Converts a Timestamp into Unix seconds by reading its fields as UTC. Milliseconds are floored because Unix seconds are integer-oriented.
@paramtimestamp Timestamp object to convert.@returnsUnix seconds.@categoryconversion
toUnixSeconds
(const timestamp: Timestamptimestamp)
function getDateTime(timestamp: Timestamp): string
Formats a Timestamp as date plus time.
@paramtimestamp Timestamp object to format.@returnsDate-time string such as `YYYY-MM-DD HH:mm`.@categoryconversion
getDateTime
(function fromUnixMilliseconds(milliseconds: number): Timestamp | null
Converts Unix milliseconds into an immutable Timestamp using UTC fields.
@parammilliseconds Unix milliseconds.@returnsTimestamp built from UTC fields, or `null` for invalid input.@categoryconversion
fromUnixMilliseconds
(const milliseconds: numbermilliseconds)!) // "2036-06-08 09:30:15.250"
function getDateTime(timestamp: Timestamp): string
Formats a Timestamp as date plus time.
@paramtimestamp Timestamp object to format.@returnsDate-time string such as `YYYY-MM-DD HH:mm`.@categoryconversion
getDateTime
(function fromUnixSeconds(seconds: number): Timestamp | null
Converts Unix seconds into an immutable Timestamp using UTC fields.
@paramseconds Unix seconds.@returnsTimestamp built from UTC fields, or `null` for invalid input.@categoryconversion
fromUnixSeconds
(const seconds: numberseconds)!) // "2036-06-08 09:30:15"

Read and refresh derived fields

Parser helpers fill these values for normal use. The explicit helpers are useful when code creates or transforms timestamp-like objects and then needs fresh derived metadata.

import {
  function getDayOfYear(timestamp: Timestamp, calendar?: CalendarSystem): number | void
Returns day of the year (doy) for the passed in Timestamp
@paramtimestamp The Timestamp to use@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsThe day of the year@categoryformatting
getDayOfYear
,
function getWeekday(timestamp: Timestamp, calendar?: CalendarSystem): number
Returns weekday for the passed in Timestamp
@paramtimestamp The Timestamp to use@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsThe weekday@categoryformatting
getWeekday
,
function getWorkWeek(timestamp: Timestamp, calendar?: CalendarSystem): number
Returns workweek for the passed in Timestamp
@paramtimestamp The Timestamp to use@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsThe work week@categoryformatting
getWorkWeek
,
function parseTimestamp(input: string, now?: Timestamp | null): Timestamp | null
Converts a supported date or date-time string into a formatted Timestamp object. If `now` is supplied, the returned timestamp also includes relative flags such as `past`, `current`, `future`, and `currentWeekday`.
@paraminput Date or date-time string, such as `YYYY-MM-DD`, `YYYY-MM-DD HH:mm:ss`, or an ISO-like value with optional milliseconds and timezone suffix.@paramnow Optional Timestamp used to calculate relative flags.@returnsFormatted Timestamp object, or `null` when the input cannot be parsed.@categoryparsing
parseTimestamp
,
function updateDayOfYear(timestamp: Timestamp, calendar?: CalendarSystem): Timestamp
Updates the Timestamp with the day of the year (doy)
@paramtimestamp The Timestamp to transform@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsA new Timestamp@categoryformatting
updateDayOfYear
,
function updateWeekday(timestamp: Timestamp, calendar?: CalendarSystem): Timestamp
Updates the Timestamp with the weekday
@paramtimestamp The Timestamp to transform@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsA new Timestamp@categoryformatting
updateWeekday
,
function updateWorkWeek(timestamp: Timestamp, calendar?: CalendarSystem): Timestamp
Updates the Timestamp with the workweek
@paramtimestamp The Timestamp to transform@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsA new Timestamp@categoryformatting
updateWorkWeek
,
} from '@timestamp-js/core' const const timestamp: Timestamptimestamp = function parseTimestamp(input: string, now?: Timestamp | null): Timestamp | null
Converts a supported date or date-time string into a formatted Timestamp object. If `now` is supplied, the returned timestamp also includes relative flags such as `past`, `current`, `future`, and `currentWeekday`.
@paraminput Date or date-time string, such as `YYYY-MM-DD`, `YYYY-MM-DD HH:mm:ss`, or an ISO-like value with optional milliseconds and timezone suffix.@paramnow Optional Timestamp used to calculate relative flags.@returnsFormatted Timestamp object, or `null` when the input cannot be parsed.@categoryparsing
parseTimestamp
('2036-06-08')!
function getWeekday(timestamp: Timestamp, calendar?: CalendarSystem): number
Returns weekday for the passed in Timestamp
@paramtimestamp The Timestamp to use@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsThe weekday@categoryformatting
getWeekday
(const timestamp: Timestamptimestamp) // 0
function getDayOfYear(timestamp: Timestamp, calendar?: CalendarSystem): number | void
Returns day of the year (doy) for the passed in Timestamp
@paramtimestamp The Timestamp to use@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsThe day of the year@categoryformatting
getDayOfYear
(const timestamp: Timestamptimestamp) // 160
function getWorkWeek(timestamp: Timestamp, calendar?: CalendarSystem): number
Returns workweek for the passed in Timestamp
@paramtimestamp The Timestamp to use@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsThe work week@categoryformatting
getWorkWeek
(const timestamp: Timestamptimestamp) // 23
function updateWeekday(timestamp: Timestamp, calendar?: CalendarSystem): Timestamp
Updates the Timestamp with the weekday
@paramtimestamp The Timestamp to transform@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsA new Timestamp@categoryformatting
updateWeekday
(const timestamp: Timestamptimestamp).Timestamp.weekday?: number | undefined
Weekday number where Sunday is `0` and Saturday is `6`.
weekday
// 0
function updateDayOfYear(timestamp: Timestamp, calendar?: CalendarSystem): Timestamp
Updates the Timestamp with the day of the year (doy)
@paramtimestamp The Timestamp to transform@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsA new Timestamp@categoryformatting
updateDayOfYear
(const timestamp: Timestamptimestamp).Timestamp.doy?: number | undefined
Day of the year.
doy
// 160
function updateWorkWeek(timestamp: Timestamp, calendar?: CalendarSystem): Timestamp
Updates the Timestamp with the workweek
@paramtimestamp The Timestamp to transform@paramcalendar Calendar system to use. Defaults to Gregorian; pass an adapter such as islamicCivilCalendar for native calendar fields.@returnsA new Timestamp@categoryformatting
updateWorkWeek
(const timestamp: Timestamptimestamp).Timestamp.workweek?: number | undefined
ISO-style workweek number.
workweek
// 23

Reuse shared constants sparingly

Constants are best when your app needs to avoid duplicate magic numbers.

import {
  const DAYS_IN_WEEK: number
Number of days in a week.
DAYS_IN_WEEK
,
const HOURS_IN_DAY: number
Number of hours in a day.
HOURS_IN_DAY
,
const MILLISECONDS_IN_DAY: number
Number of milliseconds in one day.
MILLISECONDS_IN_DAY
,
const MILLISECONDS_IN_HOUR: number
Number of milliseconds in one hour.
MILLISECONDS_IN_HOUR
,
const MILLISECONDS_IN_MINUTE: number
Number of milliseconds in one minute.
MILLISECONDS_IN_MINUTE
,
const MILLISECONDS_IN_SECOND: number
Number of milliseconds in one second.
MILLISECONDS_IN_SECOND
,
const MINUTES_IN_HOUR: number
Number of minutes in an hour.
MINUTES_IN_HOUR
,
const SECONDS_IN_DAY: number
Number of seconds in one day.
SECONDS_IN_DAY
,
const SECONDS_IN_HOUR: number
Number of seconds in one hour.
SECONDS_IN_HOUR
,
const SECONDS_IN_MINUTE: number
Number of seconds in one minute.
SECONDS_IN_MINUTE
,
const TIME_CONSTANTS: {
    MILLISECONDS_IN: {
        SECOND: number;
        MINUTE: number;
        HOUR: number;
        DAY: number;
        WEEK: number;
    };
    SECONDS_IN: {
        MINUTE: number;
        HOUR: number;
        DAY: number;
        WEEK: number;
    };
    MINUTES_IN: {
        MINUTE: number;
        HOUR: number;
        DAY: number;
        WEEK: number;
    };
    HOURS_IN: {
        DAY: number;
        WEEK: number;
    };
    DAYS_IN: {
        WEEK: number;
    };
}
Shared conversion constants for milliseconds, seconds, minutes, hours, and days.
TIME_CONSTANTS
,
} from '@timestamp-js/core' const DAYS_IN_WEEK: number
Number of days in a week.
DAYS_IN_WEEK
// 7
const HOURS_IN_DAY: number
Number of hours in a day.
HOURS_IN_DAY
// 24
const MINUTES_IN_HOUR: number
Number of minutes in an hour.
MINUTES_IN_HOUR
// 60
const SECONDS_IN_MINUTE: number
Number of seconds in one minute.
SECONDS_IN_MINUTE
// 60
const SECONDS_IN_HOUR: number
Number of seconds in one hour.
SECONDS_IN_HOUR
// 3600
const SECONDS_IN_DAY: number
Number of seconds in one day.
SECONDS_IN_DAY
// 86400
const MILLISECONDS_IN_SECOND: number
Number of milliseconds in one second.
MILLISECONDS_IN_SECOND
// 1000
const MILLISECONDS_IN_MINUTE: number
Number of milliseconds in one minute.
MILLISECONDS_IN_MINUTE
// 60000
const MILLISECONDS_IN_HOUR: number
Number of milliseconds in one hour.
MILLISECONDS_IN_HOUR
// 3600000
const MILLISECONDS_IN_DAY: number
Number of milliseconds in one day.
MILLISECONDS_IN_DAY
// 86400000
const TIME_CONSTANTS: {
    MILLISECONDS_IN: {
        SECOND: number;
        MINUTE: number;
        HOUR: number;
        DAY: number;
        WEEK: number;
    };
    SECONDS_IN: {
        MINUTE: number;
        HOUR: number;
        DAY: number;
        WEEK: number;
    };
    MINUTES_IN: {
        MINUTE: number;
        HOUR: number;
        DAY: number;
        WEEK: number;
    };
    HOURS_IN: {
        DAY: number;
        WEEK: number;
    };
    DAYS_IN: {
        WEEK: number;
    };
}
Shared conversion constants for milliseconds, seconds, minutes, hours, and days.
TIME_CONSTANTS
.
type SECONDS_IN: {
    MINUTE: number;
    HOUR: number;
    DAY: number;
    WEEK: number;
}
SECONDS_IN
.type HOUR: numberHOUR // 3600