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.
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
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
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
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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): number
Returns the days of the specified month in a year
@paramyear The year (ie: 1999, 2020)@parammonth The Gregorian month number, where January is `1`@returnsThe number of days in the month (corrected for leap years)
daysInMonth
, function isLeapYear(year: number): boolean
Returns if the passed year is a leap year
@paramyear The year to check (ie: 1999, 2020)@returnsTrue if the year is a leap year
isLeapYear
} from '@timestamp-js/core'
function isLeapYear(year: number): boolean
Returns if the passed year is a leap year
@paramyear The year to check (ie: 1999, 2020)@returnsTrue if the year is a leap year
isLeapYear
(2036) // true
function daysInMonth(year: number, month: number): number
Returns the days of the specified month in a year
@paramyear The year (ie: 1999, 2020)@parammonth The Gregorian month number, where January is `1`@returnsThe number of days in the month (corrected for leap years)
daysInMonth
(2036, 2) // 29
function daysInMonth(year: number, month: number): number
Returns the days of the specified month in a year
@paramyear The year (ie: 1999, 2020)@parammonth The Gregorian month number, where January is `1`@returnsThe number of days in the month (corrected for leap years)
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')
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')
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')
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`.
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`.
getEndOfDay
,
function getEndOfMonth(timestamp: Timestamp): 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@returnsA Timestamp of the end of the month
getEndOfMonth
,
function getEndOfWeek(timestamp: Timestamp, weekdays: number[], today: Timestamp): 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@returnsA new Timestamp representing the end of the week
getEndOfWeek
,
function getEndOfYear(timestamp: Timestamp): Timestamp
Returns a Timestamp at the end of the same Gregorian year.
@paramtimestamp Timestamp object to transform.@returnsNew Timestamp for December 31 at `23:59:59.999`.
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`.
getStartOfDay
,
function getStartOfMonth(timestamp: Timestamp): 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@returnsA Timestamp of the start of the month
getStartOfMonth
,
function getStartOfWeek(timestamp: Timestamp, weekdays: number[], today: Timestamp): 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@returnsA new Timestamp representing the start of the week
getStartOfWeek
,
function getStartOfYear(timestamp: Timestamp): Timestamp
Returns a Timestamp at the start of the same Gregorian year.
@paramtimestamp Timestamp object to transform.@returnsNew Timestamp for January 1 at `00:00`.
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.
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.
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`.
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`.
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`.
getDate
(function getStartOfWeek(timestamp: Timestamp, weekdays: number[], today: Timestamp): 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@returnsA new Timestamp representing the start of the week
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`.
getDate
(function getEndOfWeek(timestamp: Timestamp, weekdays: number[], today: Timestamp): 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@returnsA new Timestamp representing the end of the week
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`.
getDate
(function getStartOfMonth(timestamp: Timestamp): 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@returnsA Timestamp of the start of the month
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`.
getDate
(function getEndOfMonth(timestamp: Timestamp): 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@returnsA Timestamp of the end of the month
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`.
getDate
(function getStartOfYear(timestamp: Timestamp): Timestamp
Returns a Timestamp at the start of the same Gregorian year.
@paramtimestamp Timestamp object to transform.@returnsNew Timestamp for January 1 at `00:00`.
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`.
getDate
(function getEndOfYear(timestamp: Timestamp): Timestamp
Returns a Timestamp at the end of the same Gregorian year.
@paramtimestamp Timestamp object to transform.@returnsNew Timestamp for December 31 at `23:59:59.999`.
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.
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.
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`.
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.
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.
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.
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.
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.
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.
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`.
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.
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`.
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.
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): number | void
Returns day of the year (doy) for the passed in Timestamp
@paramtimestamp The Timestamp to use@returnsThe day of the year
getDayOfYear
,
function getWeekday(timestamp: Timestamp): number
Returns weekday for the passed in Timestamp
@paramtimestamp The Timestamp to use@returnsThe weekday
getWeekday
,
function getWorkWeek(timestamp: Timestamp): number
Returns workweek for the passed in Timestamp
@paramtimestamp The Timestamp to use@returnsThe work week
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.
parseTimestamp
,
function updateDayOfYear(timestamp: Timestamp): Timestamp
Updates the Timestamp with the day of the year (doy)
@paramtimestamp The Timestamp to transform@returnsA new Timestamp
updateDayOfYear
,
function updateWeekday(timestamp: Timestamp): Timestamp
Updates the Timestamp with the weekday
@paramtimestamp The Timestamp to transform@returnsA new Timestamp
updateWeekday
,
function updateWorkWeek(timestamp: Timestamp): Timestamp
Updates the Timestamp with the workweek
@paramtimestamp The Timestamp to transform@returnsA new Timestamp
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.
parseTimestamp
('2036-06-08')!
function getWeekday(timestamp: Timestamp): number
Returns weekday for the passed in Timestamp
@paramtimestamp The Timestamp to use@returnsThe weekday
getWeekday
(const timestamp: Timestamptimestamp) // 0
function getDayOfYear(timestamp: Timestamp): number | void
Returns day of the year (doy) for the passed in Timestamp
@paramtimestamp The Timestamp to use@returnsThe day of the year
getDayOfYear
(const timestamp: Timestamptimestamp) // 160
function getWorkWeek(timestamp: Timestamp): number
Returns workweek for the passed in Timestamp
@paramtimestamp The Timestamp to use@returnsThe work week
getWorkWeek
(const timestamp: Timestamptimestamp) // 23
function updateWeekday(timestamp: Timestamp): Timestamp
Updates the Timestamp with the weekday
@paramtimestamp The Timestamp to transform@returnsA new Timestamp
updateWeekday
(const timestamp: Timestamptimestamp).Timestamp.weekday?: number | undefined
Weekday number where Sunday is `0` and Saturday is `6`.
weekday
// 0
function updateDayOfYear(timestamp: Timestamp): Timestamp
Updates the Timestamp with the day of the year (doy)
@paramtimestamp The Timestamp to transform@returnsA new Timestamp
updateDayOfYear
(const timestamp: Timestamptimestamp).Timestamp.doy?: number | undefined
Day of the year.
doy
// 160
function updateWorkWeek(timestamp: Timestamp): Timestamp
Updates the Timestamp with the workweek
@paramtimestamp The Timestamp to transform@returnsA new Timestamp
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