function datevec
datevec(
d:
string
| number
| array<string | number>
,
fmt?: string,
): array | matrix

Convert date and time to an array of components.

Converts a given date and time to an array of components such as year, month, day, hour, minute, second, and millisecond. The function supports both date strings and Unix timestamps as input. Input is restricted to either a single value or an array (1D array).

Examples

Convert a date string to an array

import { assertEquals } from "jsr:@std/assert";

assertEquals(datevec('2015-01-01 03:34:05', 'YYYY-MM-DD HH:mm:ss'), [2015, 1, 1, 3, 34, 5, 0]);

Convert an array of date strings to an array of arrays

import { assertEquals } from "jsr:@std/assert";

assertEquals(datevec(['31-12-2014', '31-01-2015'], 'DD-MM-YYYY'), [[2014, 12, 31, 0, 0, 0, 0], [2015, 1, 31, 0, 0, 0, 0]]);

Convert a Unix timestamp to an array

import { assertEquals } from "jsr:@std/assert";

assertEquals(datevec(1428236430), [2015, 4, 5, 12, 20, 30, 0]);

Convert an array of Unix timestamps to an array of arrays

import { assertEquals } from "jsr:@std/assert";

assertEquals(datevec([1609459200, 1612137600]), [[2021, 1, 1, 0, 0, 0, 0], [2021, 2, 1, 0, 0, 0, 0]]);

Convert a Unix timestamp with milliseconds to an array

import { assertEquals } from "jsr:@std/assert";

assertEquals(datevec(1428236430579), [2015, 4, 5, 12, 20, 30, 579]);

Convert a date string with time zone to an array (converts to UTC)

import { assertEquals } from "jsr:@std/assert";

assertEquals(datevec('2023-08-25T14:45:00+02:00', 'YYYY-MM-DDTHH:mm:ssZ'), [2023, 8, 25, 12, 45, 0, 0]);

Convert a date string in UTC format to an array

import { assertEquals } from "jsr:@std/assert";

assertEquals(datevec('2023-08-25T14:45:00Z', 'YYYY-MM-DDTHH:mm:ssZ'), [2023, 8, 25, 14, 45, 0, 0]);

Convert an array of mixed Unix timestamps and date strings to an array of arrays

import { assertEquals } from "jsr:@std/assert";

assertEquals(datevec([1428236430, '2015-01-01 03:34:05'], 'YYYY-MM-DD HH:mm:ss'), [[2015, 4, 5, 12, 20, 30, 0], [2015, 1, 1, 3, 34, 5, 0]]);

Parameters

d:
string
| number
| array<string | number>

The date input, which can be a string, Unix timestamp, or an array of such values.

optional
fmt: string

The format string to parse the date if the input is a date string.

Return Type

An array or an array of arrays containing the date components.