date

Commons
stable

Converts a loose date value (string, Date, or undefined) to a Date object. Accepts ISO 8601 YYYY-MM-DD strings with local-timezone parsing to avoid UTC shift. Returns null for invalid or unrecognisable values.

API

Signature

Full TypeScript signature sourced from the util file.

typescript

API

Parameters

Input parameters accepted by this util.

value
any
The value to convert. When omitted or null, returns the current date (new Date()). A Date instance is returned as-is. A YYYY-MM-DD string is parsed in local time. Any other parseable string is passed to Date.parse.

API

Return value

The value returned by this util.

A Date object on success, or null when the value cannot be parsed.

Usage

Examples

Ready-to-paste usage snippets.

Parse various date inputs

typescript