create
The create() function builds a TheDate from a Date, a timestamp, or a literal partition (YYYY-MM-DD). It can also assemble a SpoolingDate (source + overrides + timezone). It returns an Either (MayBe) containing either the valid date or a typed error.
Interactive example
Syntax
typescript
function create<
GenericInput extends TheDate | Date | number
>(
input: GenericInput
): MayBe
function create<
GenericInput extends SpoolingDate
>(
input: GenericInput
): MayBe
function create<
GenericInput extends `${number}-${MonthWithDay}`
>(
input: GenericInput & YearConstraint,
params?: { hour?: Hour; minute?: Minute; second?: Second; millisecond?: Millisecond }
): TheDateYearConstraint ensures that February 29 is only accepted when the year is a leap year and that the defined year stays within supported limits.
INFO
The second declaration is only for declaring known constant dates ahead of time. Very useful for unit tests or default values.
Parameters
input: Source date. Accepts aTheDate, aDate, a timestamp, aYYYY-MM-DDstring (negative prefix accepted for BC years), or aSpoolingDate(valueasDate/timestamp/TheDate/ISOYYYY-MM-DDoptionally with time) with overrides (year,month,day,hour,minute,second,millisecond) and an optionaltimezone.params: Optional. Lets you directly provide time components (hour,minute,second,millisecond).
Return value
- Generic signature:
EitherRight<"date-created", TheDate>on success orEitherLeft<"date-created-error", null>on failure. - Literal signature: directly returns a
TheDateif the date is valid, otherwise a compile-time error. - With
SpoolingDate: returns aMayBeafter applying overrides and an optional timezone.
Supported formats
- Native
Date - Timestamp (
number) TheDate(date{timestamp}{+|-})YYYY-MM-DDstring (optionally ISO timestamp when usingSpoolingDate)SpoolingDatewithtimezoneand date/time overrides
See also
createOrThrow– Throws instead of returning anEither.isSafeTimestamp– Checks whether a timestamp is usable.
