parse()
Signature
Section titled “Signature”function parse( input: DateInput, options?: ParseOptions,): Date | nullParameters
Section titled “Parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
input | DateInput | — | String, Date, or number to parse |
options.fallback | string | null | null | Returned when parsing fails |
options.locale | string | System locale | Locale hint for ambiguous formats |
Supported formats
Section titled “Supported formats”ISO and numeric
Section titled “ISO and numeric”import { parse } from 'date-wiz';
parse('2024-05-15') // ISO 8601 → Date(2024-05-15)parse('2024-05-15T14:30') // ISO with time → Date(2024-05-15T14:30)parse('20240515') // Compact YYYYMMDD → Date(2024-05-15)parse(1715781007000) // Unix timestamp → Dateparse(new Date()) // Pass-through → clone of inputRegional formats
Section titled “Regional formats”parse('15-05-2024') // DD-MM-YYYY → Date(2024-05-15)parse('15/05/2024') // DD/MM/YYYY → Date(2024-05-15)parse('2024/05/15') // YYYY/MM/DD → Date(2024-05-15)Natural language
Section titled “Natural language”parse('May 15') // → Date(current year, May 15)parse('May 15, 2024') // → Date(2024-05-15)parse('15 May 2024') // → Date(2024-05-15)Relative shorthands
Section titled “Relative shorthands”// Offset from nowparse('+2d') // 2 days from nowparse('-1w') // 1 week agoparse('+3M') // 3 months from nowparse('-1y') // 1 year agoparse('+4h') // 4 hours from nowparse('+30m') // 30 minutes from nowparse('-45s') // 45 seconds agoparse('+500ms') // 500 milliseconds from now| Unit symbol | Meaning |
|---|---|
ms | Milliseconds |
s | Seconds |
m | Minutes |
h | Hours |
d | Days |
w | Weeks |
M | Months |
y | Years |
Named days
Section titled “Named days”parse('next_monday') // → next Monday from todayparse('next_friday') // → next Friday from todayparse('last_monday') // → most recent Mondayparse('last_saturday') // → most recent SaturdaySupported names: sunday, monday, tuesday, wednesday, thursday, friday, saturday.
Failure handling
Section titled “Failure handling”When parse cannot interpret the input, it returns null by default:
parse('not a date') // → nullparse('??/??/????') // → nullparse('not a date', { fallback: 'Invalid', // → "Invalid" (note: typed as null in most cases)})Pattern priority
Section titled “Pattern priority”Patterns are tried in this order — first match wins:
- Relative shorthand (
+2d,-1w) - Named day (
next_monday,last_friday) - Compact YYYYMMDD (
20240515) - Regional DD-MM-YYYY / DD/MM/YYYY
- Standard YYYY-MM-DD / YYYY/MM/DD
- Natural language (
May 15,May 15 2024) - Native
Dateconstructor fallback