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');// => Sat Aug 23 2025 00:00:00 GMT-0700
parse('08/23/2025', 'MM/DD/YYYY');// => Sat Aug 23 2025 00:00:00 GMT-0700
parse('23.08.2025', 'DD.MM.YYYY');// => Sat 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');// => Sat 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 |
Hundredths of a second (75 is 750 ms) | 75, 02 |
S |
Tenths of a second (7 is 700 ms) | 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?: (ParserPluginObject | 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 });// => Sat 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.
Note: A timezone name that the JavaScript runtime does not recognize makes parse() return Invalid Date, and isValid() return false, unless the input contains an offset.
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' });// => Sat 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' });// => Sat 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' });// => Sat 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' });// => Sat 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 });// => Tue Jul 08 2025 00:00:00 GMT-0700
// Bengali numeralsparse('০৮/০৭/২০২৫', 'DD/MM/YYYY', { numeral: beng });// => Tue Jul 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');// => Sat Aug 23 2025 00:00:00 GMT-0700
// Buddhist calendar (543 years behind)parse('August 23, 2568', 'MMMM D, YYYY', { calendar: 'buddhist' });// => Sat Aug 23 2025 00:00:00 GMT-0700For the Buddhist years parse() accepts, see Date Range Limitations.
hour12
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-0800An hour outside the selected range fails: h12 rejects 0, and h11 rejects 12.
hour24
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, read as 00 of the same dateparse('24:30', 'H:mm', { hour24: 'h24' });// => Thu Jan 01 1970 00:30:00 GMT-0800An hour outside the selected range fails: h23 rejects 24, and h24 rejects 0.
ignoreCase
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 });// => Sat Aug 23 2025 00:00:00 GMT-0700ignoreCase compares names using the locale’s own case rules. In Greek, where capital letters drop the accent, ΑΥΓΟΥΣΤΟΣ matches Αύγουστος. Accents that a language keeps in capitals must still match: with the French locale, AOÛT matches août, but AOUT does not.
defaultDate
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: (ParserPluginObject | 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. Unlike a format token, a parse token can only set one of the date components that the built-in parser already provides (year, month, day, hour, minute, second, millisecond, meridiem, and time zone offset); a token that sets none of them just skips the matching text. Each entry may also be a plain object literal annotated with ParserPluginObject, which rejects keys that collide with built-in tokens at compile time. Entries that are not annotated this way are not checked, so an entry that defines a built-in token such as YYYY overrides it. ParserPlugin is deprecated, kept only for compatibility with existing code, and will be removed in the next major version. See the Plugins guide for details.
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');// => Sat 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.
Defaults apply only to components that the format string does not contain. Every token in the format string must match the input, so input that ends early fails instead of being filled in:
import { parse } from 'date-and-time';
// The format string expects a day, but the input ends after the monthparse('2025-08', 'YYYY-MM-DD');// => Invalid DateDate 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 DateWith calendar: 'buddhist', the input year is a Buddhist year, and YYYY and Y read at most four digits. The parsable range is then January 1, 0544 to December 31, 9999 in Buddhist years, which is January 1, 0001 to December 31, 9456 in the Gregorian calendar.
UTC 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-0800When the input contains a 24-hour value (H or HH), it is used and h, hh, and the meridiem are ignored. A meridiem without an hour reads as 0 (AM) or 12 (PM):
parse('05 PM', 'HH A');// => Thu Jan 01 1970 05:00:00 GMT-0800
parse('PM', 'A');// => Thu Jan 01 1970 12:00:00 GMT-0800Fractional Seconds
SSS reads one to three digits as milliseconds. SS and S read a fraction of a second, so their digits are scaled: SS reads one or two digits as hundredths, and S reads exactly one digit as tenths.
parse('12', 'SSS').getMilliseconds(); // => 12parse('12', 'SS').getMilliseconds(); // => 120parse('1', 'S').getMilliseconds(); // => 100Timezone Offset Range
Z reads an offset written as +HHmm, and ZZ reads one written as +HH:mm. The offset must be between -15:56 and +15:13, the extremes found in the IANA timezone history; an offset outside this range fails.
Advanced 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';import { parser as day_of_week } from 'date-and-time/plugins/day-of-week';
parse('Today is Saturday, August 23, 2025', '[Today is] dddd, MMMM D, YYYY', { plugins: [day_of_week] });// => 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
Each space in the format string is a wildcard that matches any one character of the input, so a format string with spaces also accepts other separators in those positions ('YYYY-MM-DD HH:mm' accepts 2025-08-23T14:30). To skip part of the input, use one space per character to skip.
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-0700
// Tokens before the ellipsis must still matchparse('2025/08', 'YYYY/MM/DD...');// => Invalid DateComplex Localized Parsing
import { parse } from 'date-and-time';import fr from 'date-and-time/locales/fr';import { parser as day_of_week } from 'date-and-time/plugins/day-of-week';
// French with timezoneparse('samedi, 23 août 2025 à 14:30:45', 'dddd, D MMMM YYYY [à] HH:mm:ss', { locale: fr, timeZone: 'Europe/Paris', plugins: [day_of_week]});// => Sat Aug 23 2025 14:30:45 GMT+0200Business and Technical Formats
import { parse } from 'date-and-time';import { parser as day_of_week } from 'date-and-time/plugins/day-of-week';
// ISO 8601 formatparse('2025-08-23T14:30:45.123Z', 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]', { timeZone: 'UTC' });// => Sat 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 Z', { plugins: [day_of_week] });// => 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 mismatchparse('2025-08', 'YYYY-MM-DD'); // Invalid - input ends before the dayPerformance 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...');// => Sun 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
