parse()
Parses a formatted date string into a Date object according to the specified format pattern.
Syntax
parse(dateString, formatString[, options])Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
dateString | string | Yes | The date string to parse |
formatString | string | CompiledObject | Yes | The format pattern or compiled object |
options | ParserOptions | No | Parser options for customization |
Returns
Date - The parsed Date object, or Invalid Date if parsing fails
Basic Examples
import { parse } from 'date-and-time';
// Basic date parsingparse('2025-08-23', 'YYYY-MM-DD');// => Fri Aug 23 2025 00:00:00 GMT-0700
parse('08/23/2025', 'MM/DD/YYYY');// => Fri Aug 23 2025 00:00:00 GMT-0700
parse('23.08.2025', 'DD.MM.YYYY');// => Fri Aug 23 2025 00:00:00 GMT-0700
// Time parsingparse('14:30:45', 'HH:mm:ss');// => Thu Jan 01 1970 14:30:45 GMT-0800
parse('2:30:45 PM', 'h:mm:ss A');// => Thu Jan 01 1970 14:30:45 GMT-0800
// Combined date and timeparse('2025-08-23 14:30:45', 'YYYY-MM-DD HH:mm:ss');// => Fri Aug 23 2025 14:30:45 GMT-0700Format Tokens
Date Tokens
| Token | Description | Input Examples |
|---|---|---|
YYYY | 4-digit year | 0999, 2015 |
Y | Year without zero padding | 2, 44, 888, 2015 |
MMMM | Full month name | January, December |
MMM | Short month name | Jan, Dec |
MM | Month (01-12) | 01, 12 |
M | Month without zero padding | 1, 12 |
DD | Day (01-31) | 02, 31 |
D | Day without zero padding | 2, 31 |
Time Tokens
| Token | Description | Input Examples |
|---|---|---|
HH | Hour in 24-hour format | 23, 08 |
H | Hour in 24-hour format without zero padding | 23, 8 |
hh | Hour in 12-hour format | 11, 08 |
h | Hour in 12-hour format without zero padding | 11, 8 |
mm | Minutes | 14, 07 |
m | Minutes without zero padding | 14, 7 |
ss | Seconds | 05, 10 |
s | Seconds without zero padding | 5, 10 |
SSS | 3-digit milliseconds | 753, 022 |
SS | 2-digit milliseconds | 75, 02 |
S | 1-digit milliseconds | 7, 0 |
AM/PM Tokens
| Token | Description | Input Examples |
|---|---|---|
A | Uppercase AM/PM | AM, PM |
AA | Uppercase AM/PM (with periods) | A.M., P.M. |
a | Lowercase AM/PM | am, pm |
aa | Lowercase AM/PM (with periods) | a.m., p.m. |
Timezone Tokens
| Token | Description | Input Examples |
|---|---|---|
Z | Timezone offset | +0100, -0800 |
ZZ | Timezone offset with colon | +01:00, -08:00 |
Plugin Tokens
Additional tokens available with plugins:
| Token | Description | Input Examples | Plugin Required |
|---|---|---|---|
YY | 2-digit year | 90, 00, 08, 19 | two-digit-year |
DDD | Ordinal representation | 1st, 2nd, 3rd | ordinal |
dddd | Full day name | Friday, Sunday | day-of-week |
ddd | Short day name | Fri, Sun | day-of-week |
dd | Very short day name | Fr, Su | day-of-week |
SSSSSS | 6-digit milliseconds | 123456, 000001 | microsecond |
SSSSS | 5-digit milliseconds | 12345, 00001 | microsecond |
SSSS | 4-digit milliseconds | 1234, 0001 | microsecond |
fff | 3-digit microseconds | 753, 022 | microsecond |
ff | 2-digit microseconds | 75, 02 | microsecond |
f | 1-digit microseconds | 7, 0 | microsecond |
SSSSSSSSS | 9-digit milliseconds | 123456789, 000000001 | nanosecond |
SSSSSSSS | 8-digit milliseconds | 12345678, 00000001 | nanosecond |
SSSSSSS | 7-digit milliseconds | 1234567, 0000001 | nanosecond |
FFF | 3-digit nanoseconds | 753, 022 | nanosecond |
FF | 2-digit nanoseconds | 75, 02 | nanosecond |
F | 1-digit nanoseconds | 7, 0 | nanosecond |
For available plugins, see the plugins option in ParserOptions.
ParserOptions
The ParserOptions object allows you to customize the parsing behavior:
interface ParserOptions { locale?: Locale; timeZone?: TimeZone | string; numeral?: Numeral; calendar?: 'gregory' | 'buddhist'; hour12?: 'h11' | 'h12'; hour24?: 'h23' | 'h24'; ignoreCase?: boolean; defaultDate?: ParsedComponents; plugins?: ParserPlugin[];}locale
Type: Locale
Default: en (English)
Specifies the locale for parsing localized month names, day names, and meridiem indicators.
import { parse } from 'date-and-time';import es from 'date-and-time/locales/es';
// Spanish parsingparse('23 de agosto de 2025', 'D [de] MMMM [de] YYYY', { locale: es });// => Fri Aug 23 2025 00:00:00 GMT-0700For a complete list of all supported locales with import examples, see Supported Locales.
timeZone
Type: TimeZone | string
Default: undefined (local timezone)
Interprets the parsed date in the specified timezone.
Note: If the input string contains a timezone offset (e.g., Z or ZZ tokens), that offset takes precedence and the timeZone option is ignored.
import { parse } from 'date-and-time';
// Parse using an IANA timezone name stringparse('2025-08-23 14:30:00', 'YYYY-MM-DD HH:mm:ss', { timeZone: 'Asia/Tokyo' });// => Fri Aug 23 2025 14:30:00 GMT+0900
// Parse in UTCparse('2025-08-23 14:30:00', 'YYYY-MM-DD HH:mm:ss', { timeZone: 'UTC' });// => Fri Aug 23 2025 14:30:00 GMT+0000
// Timezone offset in input takes precedence over timeZone optionparse('2025-08-23 14:30:00 +0300', 'YYYY-MM-DD HH:mm:ss Z', { timeZone: 'Asia/Tokyo' });// => Fri Aug 23 2025 14:30:00 GMT+0300 (Asia/Tokyo timeZone is ignored)
parse('2025-08-23T14:30:00 +05:00', 'YYYY-MM-DD[T]HH:mm:ss ZZ', { timeZone: 'America/New_York' });// => Fri Aug 23 2025 14:30:00 GMT+0500 (America/New_York timeZone is ignored)For a complete list of all supported timezones, see Supported Timezones.
numeral
Type: Numeral
Default: latn (Latin numerals)
Specifies the numeral system for parsing numbers.
import { parse } from 'date-and-time';import arab from 'date-and-time/numerals/arab';import beng from 'date-and-time/numerals/beng';
// Arabic-Indic numeralsparse('٠٨/٠٧/٢٠٢٥', 'DD/MM/YYYY', { numeral: arab });// => Fri Aug 08 2025 00:00:00 GMT-0700
// Bengali numeralsparse('০৮/০৭/২০২৫', 'DD/MM/YYYY', { numeral: beng });// => Fri Aug 08 2025 00:00:00 GMT-0700Available numeral systems:
latn- Latin numerals (0-9) - defaultarab- Arabic-Indic numerals (٠-٩)arabext- Extended Arabic-Indic numerals (۰-۹)beng- Bengali numerals (০-৯)mymr- Myanmar numerals (၀-၉)
calendar
Type: "gregory" | "buddhist"
Default: "gregory"
Specifies the calendar system for date calculations.
import { parse } from 'date-and-time';
// Gregorian calendar (default)parse('August 23, 2025', 'MMMM D, YYYY');// => Fri Aug 23 2025 00:00:00 GMT-0700
// Buddhist calendar (543 years behind)parse('August 23, 2568', 'MMMM D, YYYY', { calendar: 'buddhist' });// => Fri Aug 23 2025 00:00:00 GMT-0700hour12
Type: "h11" | "h12"
Default: "h12"
Controls the 12-hour format interpretation. Use h11 for 11-hour format (0-11) or h12 for 12-hour format (1-12).
import { parse } from 'date-and-time';
// h12 format - midnight is 12 AMparse('12:30 AM', 'h:mm A', { hour12: 'h12' });// => Thu Jan 01 1970 00:30:00 GMT-0800
// h11 format - midnight is 0 AMparse('0:30 AM', 'h:mm A', { hour12: 'h11' });// => Thu Jan 01 1970 00:30:00 GMT-0800hour24
Type: "h23" | "h24"
Default: "h23"
Controls the 24-hour format interpretation. Use h23 for 23-hour format (0-23) or h24 for 24-hour format (1-24).
import { parse } from 'date-and-time';
// h23 format - midnight is 0parse('0:30', 'H:mm', { hour24: 'h23' });// => Thu Jan 01 1970 00:30:00 GMT-0800
// h24 format - midnight is 24 (of previous day)parse('24:30', 'H:mm', { hour24: 'h24' });// => Thu Jan 01 1970 00:30:00 GMT-0800ignoreCase
Type: boolean
Default: false
Enables case-insensitive parsing for text elements.
import { parse } from 'date-and-time';
// Case-sensitive (default)parse('august 23, 2025', 'MMMM D, YYYY');// => Invalid Date
// Case-insensitiveparse('AUGUST 23, 2025', 'MMMM D, YYYY', { ignoreCase: true });// => Fri Aug 23 2025 00:00:00 GMT-0700
parse('fri aug 23 2025', 'ddd MMM DD YYYY', { ignoreCase: true });// => Fri Aug 23 2025 00:00:00 GMT-0700defaultDate
Type: ParsedComponents
Default: { Y: 1970, M: 1, D: 1, m: 0, s: 0, S: 0 }
Specifies default values for date/time components that are missing from the format string. This is useful when parsing partial strings such as time-only or month-day formats.
interface ParsedComponents { Y?: number; // Year M?: number; // Month (1-12) D?: number; // Day H?: number; // Hour (24-hour) A?: number; // Meridiem (0: AM, 1: PM) h?: number; // Hour (12-hour) m?: number; // Minute s?: number; // Second S?: number; // Millisecond Z?: number; // Timezone offset in minutes (e.g., UTC+9 = -540)}Note: If defaultDate.Z is set, it takes precedence over the timeZone option. Z is in minutes, using the same sign convention as the Z / ZZ format tokens (e.g., UTC+9 = -540).
import { parse } from 'date-and-time';
// Parse time-only string — fill in date from defaultDateparse('12:30', 'HH:mm', { defaultDate: { Y: 2024, M: 3, D: 15 } });// => Fri Mar 15 2024 12:30:00
// Parse month-day only — fill in year from defaultDateparse('03-15', 'MM-DD', { defaultDate: { Y: 2024 } });// => Fri Mar 15 2024 00:00:00
// Fill in time components for a date-only formatparse('2024-03-15', 'YYYY-MM-DD', { defaultDate: { H: 10, m: 30, s: 45 } });// => Fri Mar 15 2024 10:30:45
// defaultDate.Z takes precedence over timeZone optionparse('12:30', 'HH:mm', { defaultDate: { Y: 2024, M: 3, D: 15, Z: -540 }, timeZone: 'UTC' });// => Fri Mar 15 2024 03:30:00 UTC (interpreted as UTC+9; timeZone: 'UTC' is ignored)plugins
Type: ParserPlugin[]
Default: undefined
Enables additional parse tokens provided by plugins. Plugins extend the parser with special tokens that are not included in the core library.
import { parse } from 'date-and-time';import { parser as ordinal } from 'date-and-time/plugins/ordinal';import { parser as two_digit_year } from 'date-and-time/plugins/two-digit-year';import { parser as microsecond } from 'date-and-time/plugins/microsecond';
// Use ordinal pluginparse('January 1st, 2025', 'MMMM DDD, YYYY', { plugins: [ordinal] });// => Wed Jan 01 2025 00:00:00 GMT-0800
// Use two-digit-year pluginparse('12/25/99', 'MM/DD/YY', { plugins: [two_digit_year] });// => Sat Dec 25 1999 00:00:00 GMT-0800
// Use microsecond pluginparse('14:30:45.123456', 'HH:mm:ss.SSSSSS', { plugins: [microsecond] });// => Thu Jan 01 1970 14:30:45 GMT-0800
// Use multiple plugins togetherparse('January 1st, 99 14:30:45.123456', 'MMMM DDD, YY HH:mm:ss.SSSSSS', { plugins: [ordinal, two_digit_year, microsecond]});// => Fri Jan 01 1999 14:30:45 GMT-0800For a complete list of available plugins, see Plugins.
Parsing Behavior and Limitations
Default Date and Time Values
When parsing partial dates or times, missing components are filled with default values. The default date is January 1, 1970, and the default time is 00:00:00.000.
import { parse } from 'date-and-time';
// Only time - defaults to Jan 1, 1970parse('14:30:45', 'HH:mm:ss');// => Thu Jan 01 1970 14:30:45 GMT-0800
// Only date - defaults to 00:00:00parse('2025-08-23', 'YYYY-MM-DD');// => Fri Aug 23 2025 00:00:00 GMT-0700
// Year and month - defaults to 1st dayparse('2025-08', 'YYYY-MM');// => Fri Aug 01 2025 00:00:00 GMT-0700
// Just year - defaults to Jan 1st, 00:00:00parse('2025', 'YYYY');// => Wed Jan 01 2025 00:00:00 GMT-0800To customize these defaults, use the defaultDate option.
Date Range Limitations
The parsable maximum date is December 31, 9999, and the minimum date is January 1, 0001.
import { parse } from 'date-and-time';
// Valid maximum dateparse('Dec 31 9999', 'MMM D YYYY');// => Fri Dec 31 9999 00:00:00 GMT-0800
// Invalid - exceeds maximumparse('Dec 31 10000', 'MMM D YYYY');// => Invalid Date
// Valid minimum dateparse('Jan 1 0001', 'MMM D YYYY');// => Mon Jan 1 0001 00:00:00 GMT-0800
// Invalid - below minimumparse('Jan 1 0000', 'MMM D YYYY');// => Invalid DateUTC Input Parsing
If the input string doesn’t contain a timezone offset and no timeZone option is specified, the function treats the input as the local timezone. To parse input as UTC, set timeZone: 'UTC' in the options.
import { parse } from 'date-and-time';
// Parsed as the local timezoneparse('14:30:45', 'HH:mm:ss');// => Thu Jan 01 1970 14:30:45 GMT-0800
// Timezone offset in input takes precedenceparse('14:30:45 +0000', 'HH:mm:ss Z');// => Thu Jan 01 1970 14:30:45 GMT+0000
// Force UTC parsingparse('14:30:45', 'HH:mm:ss', { timeZone: 'UTC' });// => Thu Jan 01 1970 14:30:45 GMT+000012-hour Format and Meridiem
When using 12-hour format tokens (h or hh), always include the meridiem token (A or a) for correct parsing.
import { parse } from 'date-and-time';
// Without meridiem - ambiguous timeparse('11:30:45', 'h:mm:ss');// => Thu Jan 01 1970 11:30:45 GMT-0800 (assumes AM)
// With meridiem - unambiguous timeparse('11:30:45 PM', 'h:mm:ss A');// => Thu Jan 01 1970 23:30:45 GMT-0800Advanced Usage
Comments in Format Strings
Parts of the format string enclosed in square brackets are treated as literal text:
import { parse } from 'date-and-time';
parse('Today is Saturday, August 23, 2025', '[Today is] dddd, MMMM D, YYYY');// => Sat Aug 23 2025 00:00:00 GMT-0700
parse('2025-08-23T14:30:45Z', 'YYYY-MM-DD[T]HH:mm:ss[Z]');// => Sat Aug 23 2025 14:30:45 GMT-0700
parse('Report generated on 2025/08/23 at 14:30', '[Report generated on] YYYY/MM/DD [at] HH:mm');// => Sat Aug 23 2025 14:30:00 GMT-0700
// Escape square brackets to parse them from input stringparse('[2025-08-23 14:30:45]', '\\[YYYY-MM-DD HH:mm:ss\\]');// => Sat Aug 23 2025 14:30:45 GMT-0700Wildcard Parsing
Whitespace in the format string acts as a wildcard token, allowing you to skip corresponding parts of the input string. The character count must match between the format string and input string.
import { parse } from 'date-and-time';
// This will fail - extra content not accounted forparse('2025/08/23 14:30:45', 'YYYY/MM/DD');// => Invalid Date
// Use whitespace as wildcard (9 spaces to match ' 14:30:45')parse('2025/08/23 14:30:45', 'YYYY/MM/DD ');// => Sat Aug 23 2025 00:00:00 GMT-0700Ellipsis Token
The ... token ignores all subsequent content in the input string. Use this token only at the end of a format string.
import { parse } from 'date-and-time';
// Ignore everything after the dateparse('2025/08/23 14:30:45', 'YYYY/MM/DD...');// => Sat Aug 23 2025 00:00:00 GMT-0700
// More complex exampleparse('Log entry: 2025-08-23 some extra data here', '[Log entry: ]YYYY-MM-DD...');// => Sat Aug 23 2025 00:00:00 GMT-0700Complex Localized Parsing
import { parse } from 'date-and-time';import fr from 'date-and-time/locales/fr';
// French with timezoneparse('samedi, 23 août 2025 à 14:30:45', 'dddd, D MMMM YYYY [à] HH:mm:ss', { locale: fr, timeZone: 'Europe/Paris'});// => Fri Aug 23 2025 14:30:45 GMT+0200Business and Technical Formats
import { parse } from 'date-and-time';
// ISO 8601 formatparse('2025-08-23T14:30:45.123Z', 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]', { timeZone: 'UTC' });// => Fri Aug 23 2025 14:30:45 GMT+0000
// RFC 2822 formatparse('Sat, 23 Aug 2025 14:30:45 +0900', 'ddd, DD MMM YYYY HH:mm:ss ZZ');// => Sat Aug 23 2025 14:30:45 GMT+0900
// File naming formatparse('20250823_143045', 'YYYYMMDD_HHmmss');// => Sat Aug 23 2025 14:30:45 GMT-0700Error Handling
When Parsing Fails
If the parse() function fails to parse the input, it returns an Invalid Date object. Note that Invalid Date is still a Date object, not NaN or null.
import { parse } from 'date-and-time';
// Example of parsing failureconst result = parse('invalid-date', 'YYYY-MM-DD');
// Check if parsing failedif (isNaN(result.getTime())) { console.error('Parse failed - invalid date');} else { console.log('Successfully parsed:', result);}
// Common parsing failuresparse('Jam 1 2017', 'MMM D YYYY'); // Invalid - 'Jam' is not a valid monthparse('2025-02-30', 'YYYY-MM-DD'); // Invalid - Feb 30th doesn't existparse('2025-13-01', 'YYYY-MM-DD'); // Invalid - month 13 doesn't existparse('25:30:00', 'HH:mm:ss'); // Invalid - hour 25 doesn't existparse('12 hours 34', 'HH hours mm'); // Invalid - format mismatchPerformance Considerations
For repeated parsing with the same pattern, use compile():
import { parse, compile } from 'date-and-time';
// Compile onceconst pattern = compile('YYYY-MM-DD HH:mm:ss');
// Reuse for better performanceconst dateStrings = ['2025-08-23 14:30:45', '2025-08-24 09:15:30'];dateStrings.forEach(dateString => { const parsed = parse(dateString, pattern); console.log(parsed);});Common Parsing Patterns
Log Timestamps
import { parse } from 'date-and-time';
const logLine = '[2025-08-23 14:30:45.123] Application started';const timestamp = parse(logLine, ' YYYY-MM-DD HH:mm:ss.SSS ...');// => Sat Aug 23 2025 14:30:45 GMT-0700
// For different log formatsconst syslogLine = 'Aug 23 14:30:45 server: Process started';const syslogTimestamp = parse(syslogLine, 'MMM DD HH:mm:ss...');// => Sat Aug 23 1970 14:30:45 GMT-0700API Responses
import { parse } from 'date-and-time';
const apiTimestamp = '2025-08-23T14:30:45Z';const date = parse(apiTimestamp, 'YYYY-MM-DD[T]HH:mm:ss[Z]', { timeZone: 'UTC' });User Input
import { parse } from 'date-and-time';
function parseUserDate(input: string) { const formats = [ 'YYYY-MM-DD', 'MM/DD/YYYY', 'DD.MM.YYYY', 'MMMM D, YYYY' ];
for (const format of formats) { const result = parse(input, format); if (!isNaN(result.getTime())) { return result; } }
throw new Error('Unable to parse date');}See Also
compile()- Precompile format patterns for performancepreparse()- Parse and return intermediate parsing resultsisValid()- Validate date string formats