A library of TypeScript types related to Javascript Date.
The types add type safety to various strings and numbers used to serialize or calculate with Date values. This provides support for conversion and validation, but only minimal support for calculation.
This library has no runtime dependencies.
This is a union type of built-in representations of a moment in time.
It includes Date
, number
of milliseconds since UNIX epoch, and string
ISO timestamps.
Returns or constructs a Date
object from DateLike
.
This is a type for string
s obtained from Date.toISOString()
.
Type-guard for x
being a valid string
representing a Timestamp
.
Returns a Timestamp
representing the current moment in time.
Returns x
if it represents a valid Timestamp
string, null
otherwise.
Returns x
if it represents a valid Timestamp
string, and throws otherwise.
Converts a valid DateLike
to Timestamp
.
DurationMs is a type for number
s that represents duration as a number of milliseconds.
Type-guard for x
being a valid number
that represents a DurationMs
.
Succeeds for any number
.
Returns a DurationMs
given a DurationSpec
:
export interface DurationSpec {
days?: number;
hours?: number;
minutes?: number;
seconds?: number;
ms?: number;
}
Negative values are supported.
EpochMs
is a type for number
s that represents a number of milliseconds since the UNIX epoch (midnight, Jan 1, 1970, UTC).
This has the same semantics as Date.now()
.
Type-guard for x
being a valid number
that represents an EpochMs
.
Succeeds for any number
.
Returns an EpochMs
representing the current moment in time.
Converts a valid DateLike
to EpochMs
.
A namespace of functions for calculating with DurationMs
and EpochMs
.
Returns base + duration
, works for negative values.
Returns end - start
, works for negative values.
Note the order is opposite subtraction--if the earlier EpochMs
comes first the result is positive.
Returns the sum of zero or more DurationMs
.
Negates a DurationMs
.
Returns the maximum of DurationMs
s or EpochMs
s.
Note, arguments must all be of the same type.
Returns the minimum of DurationMs
s or EpochMs
s.
Note, arguments must all be of the same type.
CalendarDay
is a type for string
s that represents a particular calendar day in the form "YYYY-MM-DD".
It is a goal that this type be sortable, so values are restricted to positive years with 4 digits.
A Timestamp
representing the earliest valid CalendarDay
.
A Timestamp
representing the latest valid CalendarDay
.
An EpochMs
representing the earliest valid CalendarDay
.
An EpochMs
representing the latest valid CalendarDay
.
For use in date calculations via EpochMs
and DurationMs
.
Returns an EpochMs
between MIN_EPOCH_MS
and MAX_EPOCH_MS
inclusive that represents the closest EpochMs
that falls in the range of valid CalendarDay
s.
Type-guard for x
being a valid string
that represents a CalendarDay
.
Returns x
if it represents a valid CalendarDay
string, null
otherwise.
Returns x
if it represents a valid CalendarDay
string, and throws otherwise.
Converts a valid DateLike
to CalendarDay
.
Experimental.
These have the same semantics as CalendarDay
except for month and year respectively, and support the same set of functions.