Skip to content

Function.parseDate

github-actions[bot] edited this page Nov 18, 2026 · 13 revisions

umt / parseDate

Function: parseDate()

parseDate(string, formatString): Date

Defined in: Date/parseDate.ts:166

Parses a string into a Date according to a format pattern. Uses the same tokens as format: YYYY, YY, MM, M, DD, D, d, HH, H, hh, h, mm, m, ss, s, SSS, A, a, Z, ZZ, and [text] for escaped literal text. Any other character in the pattern must match literally.

  • YY is read as 2000-2099.
  • Fields missing from the pattern default to 1970-01-01 00:00:00.000.
  • hh / h are converted with A / a when present (12 AM is 0, 12 PM is 12); without a meridiem token the hour is used as written.
  • d (day of week, 0 = Sunday) must agree with the parsed calendar date.
  • With Z / ZZ, the fields are read in that offset; otherwise in local time.
  • When a token appears more than once, the last occurrence wins.

Returns an Invalid Date when the string does not match the pattern or a field is out of range (month 1-12, day within the month, hour 0-23 or 1-12 for 12-hour tokens, minute and second 0-59, offset hours 0-23 and minutes 0-59).

Parameters

string

string

The date string to parse

formatString

string

The format pattern string

Returns

Date

The parsed Date, or an Invalid Date when parsing fails

Example

parseDate("2025-04-04", "YYYY-MM-DD"); // 2025-04-04T00:00:00 local time
parseDate("04/04/2025 3:30 PM", "MM/DD/YYYY h:mm A"); // 2025-04-04T15:30:00 local time
parseDate("2025-04-04T15:30:00+09:00", "YYYY-MM-DDTHH:mm:ssZ"); // 2025-04-04T06:30:00Z
parseDate("2025-13-01", "YYYY-MM-DD"); // Invalid Date

API

Classes

Interfaces

Type Aliases

Variables

Functions

Clone this wiki locally