datetime-parse() #

number datetime-parse(string format, string datetime)

Parses the given datetime according to format and returns the corresponding Unix timestamp.

Parts of datetime not covered by format are filled with default values: year = 1970, month = 1, day = 1, time = 00:00:00.0, timezone = value of the system timezone set by the TZ environment variable (defaults to UTC).

The system timezone is ignored if datetime either is a UNIX timestamp or specifies a timezone itself (e.g. 1. Jan 1970 CET → -3600).

datetime-parse() returns NaN if parsing fails.

Overflows may occur if a given date is out of range (e.g. 33 December 2022 → 2 January 2023)

Examples #

<!-- environment variable TZ="" or TZ="UTC" → 0 -->
<eval out="$jan-1st-1970">datetime-parse('Y-m-d', '1970-01-01')</eval>

<!-- TZ="CET" → -3600 -->
<eval out="$jan-1st-1970">datetime-parse('Y-m-d', '1970-01-01')</eval>

<!-- TZ="CET", but UTC given in datetime → 0 -->
<eval out="$jan-1st-1970">datetime-parse('Y-m-d T', '1970-01-01 UTC')</eval>

<!-- → 1000000000 -->
<eval out="$unix-billennium">datetime-parse('d M Y H:i:s T', '09 Sep 2001 01:46:40 UTC')</eval>

Format #

Escape any of the below formatting letters with a backslash (\) to parse them literally. Supported separators are: ;, :, /, ., ,, -, (, ).

Format Description Values/Examples
a ante/post meridiem am, P.M., …
A → a
d day of the month (1 or 2 digits) 01 … 31 or 1 … 31
D day of the week (3 letters or word, English) Mon … Sun or Monday … Sunday
e → T
F → M
g → h
G → H
h hour (12-hour format, 1 or 2 digits) 1 … 12, 01 … 12
H hour (24-hour format, 1 or 2 digits) 0 … 23, 00 … 23
i minutes (2 digits) 00 … 59
j → d
l → D
m month (1 or 2 digits) 1 … 12, 01 … 12
M month (textual, English) January … December, Jan … Dec, Sept
n → m
O → T
P → T
s seconds (2 digits) 00 … 59
S ordinal day suffix (2 letters, English) st, nd, rd, th
T timezone identifier or abbreviation, offset from UTC CET, Europe/Berlin, +06:00, -0200 Z, …
u microseconds (up to 6 digits) 00, 123, 456789, …
U seconds since the Unix Epoch 0 (= January 1st 1970 00:00:00 UTC), …
v milliseconds (up to 3 digits) 0, 12, 034, …
Y year (up to 4 digits) 1999, 2023, …
y year (2 digits, 1970–2069) 00 (= 2000), 70 (= 1970), 69 (= 2069), …
z day of the year 0 (= January 1st) … 365
Z timezone offset in seconds -43200 … 50400
␣ (space) zero or more space or tab characters
? a single byte
* one or more bytes

See also #