Skip to content

Overview

ZDate has one parser shared by the ZDate constructor and the lower-level ZEpoch API.

  • ZEpoch.parse() invokes the parser directly and returns the resolved nanosecond epoch as a bigint. It does not create a ZDate object.
  • ZEpoch.parseSpec() invokes the parser directly and returns the epoch, detected specification, and any parsed suffix data. It does not create a ZDate object.
  • The ZDate constructor, or function-style zdate(), uses the same parser to resolve a string source before creating the instance. Its views remain lazy.
ts
import {zdate, ZDate, ZEpoch, ParsedSpecResult} from '@sajajs/zdate'

const epoch: bigint = ZEpoch.parse('2024-01-15T12:00:00Z')
const parsed: ParsedSpecResult = ZEpoch.parseSpec('2024-01-15T12:00:00Z')
const date1: ZDate = new ZDate('2024-01-15T12:00:00Z')
const date2: ZDate = zdate('2024-01-15T12:00:00Z')

The sections below document each accepted format and the spec tag returned by ZEpoch.parseSpec().

One Parser, One Doorway

ZDate accepts every supported date string through the same parser. Callers do not choose between ISO 8601, RFC 3339, RFC 9557, Internet headers, log formats, or natural dates. Pass the source and ZDate detects the format.

ts
const date = zdate('Sun, 06 Nov 1994 08:49:37 GMT') // rfc9110

Use ZEpoch.parse() when only the resolved epoch is needed:

ts
const epoch = ZEpoch.parse('2024-01-15') // iso

When the detected format matters, use ZEpoch.parseSpec():

ts
const {epoch, spec, suffix} = ZEpoch.parseSpec('2024-01-15T12:00:00-05:00[America/New_York]')

spec         // 'rfc9557'
suffix?.zone // 'America/New_York'

The input formats below show what can enter that doorway. The dedicated sections document each format's boundaries, extensions, and resolution rules.

ParseSpecResult

ZEpoch.parseSpec() returns a ParseSpecResult:

ts
export type ParseSpecResult = {
    epoch: bigint
    spec: ParsedSpecId
    suffix?: ParsedSuffix
}
FieldDescription
epochThe resolved instant as nanoseconds from the Unix epoch
specThe specification or syntax recognized by the parser
suffixRFC 9557 zone, calendar, critical markers, and extension tags

The suffix field is present for RFC 9557 input:

ts
import {ZEpoch} from '@sajajs/zdate'

const result = ZEpoch.parseSpec(
    '2024-01-15T12:00:00-05:00[America/New_York][u-ca=hebrew]'
)

result.epoch                      // 1705338000000000000n
result.spec                       // 'rfc9557'
result.suffix?.zone               // 'America/New_York'
result.suffix?.zoneRequired       // false
result.suffix?.calendar           // 'hebrew'
result.suffix?.calendarRequired   // false
result.suffix?.tags               // {'u-ca': 'hebrew'}

ISO 8601

The zdate parser tags this format as iso.

ISO 8601 defines numeric representations for calendar dates, times, offsets, week dates, ordinal dates, and related date/time values. ZDate accepts the common extended forms, compact forms, expanded years, and a few documented extensions.

Input Formats

FormExampleNotes
Year and month2024-01Day defaults to 1
Calendar date2024-01-15Time defaults to midnight UTC
Extended timestamp2024-01-15T12:00:00.123456789+0530Colonless offset keeps ISO classification
Compact date20240115Separators are omitted
Compact timestamp20240115T120000.123456789ZSeparators are omitted
Expanded year+012024-01-15T12:00:00+0530Expanded years require a sign
SQL datetime extension2024-01-15 12:00:00A space may replace T

Rules

ISO 8601 Week and Ordinal Dates

The zdate parser tags these formats as iso-week-ordinal.

ISO 8601 also defines dates by week number or day of year. ZDate resolves both forms to the same epoch model used by calendar dates.

Input Formats

FormExampleNotes
Ordinal date2024-015Day 15 of the year
Ordinal timestamp2024-015T12:00:00Z
Week date2024-W03-1Monday of ISO week 3
Week timestamp2024-W03-1T12:00:00Z

Rules

RFC 3339

The zdate parser tags this format as rfc3339.

RFC 3339 defines an Internet timestamp profile of ISO 8601. It uses a four-digit year, a complete date and time separated by T, and either Z or a +/-HH:MM offset.

Input Formats

FormExampleNotes
UTC2024-01-15T12:00:00Z
Numeric offset2024-01-15T12:00:00+05:30Offset requires a colon
Fractional seconds2024-01-15T12:00:00.123456789-05:00ZDate preserves nanosecond precision

Rules

RFC 9557

The zdate parser tags this format as rfc9557.

RFC 9557 extends an RFC 3339 timestamp with bracketed time-zone, calendar, and extension information. ZDate also accepts a zone-only form and Java's exact offset-seconds form as documented extensions.

Input Formats

FormExampleNotes
Zone suffix2024-01-15T12:00:00-05:00[America/New_York]Offset and zone must agree
Calendar suffix2024-01-15T12:00:00Z[u-ca=hebrew]Calendar is suffix metadata
Zone-only extension2024-01-15T12:00:00[America/New_York]Zone resolves the wall time
Java offset-seconds extension1883-11-17T12:00:00-04:56:02[America/New_York]Offset seconds require a suffix

Rules

RFC 5322

The zdate parser tags this format as rfc5322.

RFC 5322 defines the date and time syntax used by Internet messages. The same parser accepts the related RFC 2822 and RFC 1123 forms.

Input Formats

FormExampleNotes
Numeric offsetMon, 15 Jan 2024 12:00:00 -0500
No weekday15 Jan 2024 12:00:00 GMTWeekday is optional
Zone abbreviation15 Jan 2024 12:00:00 ESTKnown abbreviations are resolved

Rules

RFC 9110

The zdate parser tags this format as rfc9110.

RFC 9110 defines IMF-fixdate, the preferred HTTP date format. It always uses GMT.

Input Formats

FormExampleNotes
IMF-fixdateSun, 06 Nov 1994 08:49:37 GMTAlways GMT

Rules

RFC 850 HTTP Date

The zdate parser tags this format as rfc850.

RFC 9110 retains the obsolete RFC 850 date shape for compatibility with older HTTP senders. This is the fixed-GMT HTTP form, not RFC 850's broader original Usenet date syntax.

Input Formats

FormExampleNotes
Obsolete HTTP dateSunday, 06-Nov-94 08:49:37 GMTFull weekday and two-digit year

Rules

Generalized Time

The zdate parser tags this format as generalized-time.

RFC 4517 defines the LDAP encoding of ASN.1 GeneralizedTime. It uses a compact timestamp followed by Z or a numeric offset.

Input Formats

FormExampleNotes
Hour2024011512ZOmitted fields default to zero
Minute202401151230ZSeconds default to zero
Second20240115123045Z
Fractional second20240115123045.123456789ZFraction requires seconds
Numeric offset202401150700-0500Offset or Z is required

Rules

Common Log Format

The zdate parser tags this format as commonlog.

The Common Log Format timestamp is used in traditional web-server access logs. It combines a named month with a required numeric offset.

Input Formats

FormExampleNotes
Common Log Format15/Jan/2024:12:00:00 +0000Numeric offset is required
Single-digit day6/Nov/1994:08:49:37 -0500Day may be one or two digits

Rules

asctime()

The zdate parser tags this format as asctime.

The C asctime() shape contains a weekday, named month, local clock time, and year, but no offset. ZDate treats it as UTC.

Input Formats

FormExampleNotes
Space-padded daySun Nov 6 08:49:37 1994No offset; treated as UTC
Two-digit dayMon Jan 15 12:00:00 2024No offset; treated as UTC

Rules

Natural Dates

The zdate parser tags this format as natural.

Natural dates are ZDate's relative and conversational date specification. They are resolved against an anchor instant using the requested view options.

Input Formats

FormExampleNotes
AnchornowAnchored to the current instant
Relative daytomorrow at noon
Relative amount2 hours ago
Relative weekdaynext friday at 10am
Ordinal weekdayfirst friday of month
Absolute datejan 15 at noonMissing year comes from the anchor

Rules

Custom Formats

Custom formats are parsed by ZEpoch.parseFormat() and do not receive a parseSpec() tag.

Custom parsing accepts a caller-supplied format and optional locale. It always uses the ISO 8601 calendar.

Input Formats

FormatExampleNotes
YYYY-MM-DD2024-01-15Caller supplies the format
MMMM DD, YYYYJanuary 15, 2024Locale month name
YYYY-MM-DD HH:mm:ss.fff2024-01-15 12:00:00.123456789Nanosecond fraction
DD MMMM YYYY HH:mm with fr-FR14 juillet 2025 14:30Caller supplies the locale

Rules