javascript
  1. javascript-date-parse-method

JavaScript Date parse() method

The parse() method of the JavaScript Date object is used to parse a date string and returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC.

Syntax

The syntax for the parse() method is as follows:

Date.parse(dateString)

Here, dateString is the date string that should be parsed. The date string should be in a format recognized by the Date.parse() method.

Example

Consider the following example:

const dateString = '2022-10-31T02:30:00.000Z';
const milliseconds = Date.parse(dateString);
console.log(milliseconds);

Output:

1667225400000

Explanation

In the above example, the dateString represents the date and time in ISO format. The Date.parse() method parses the date string and returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC.

Use

The parse() method is useful when you need to convert a date string into a Date object or when you need to compare two dates.

Important Points

  • The parse() method returns NaN if the date string cannot be parsed.
  • The input date string should be in a format recognized by the Date.parse() method. Some commonly recognized formats are: YYYY-MM-DDTHH:mm:ss.sssZ and MM/DD/YYYY HH:mm:ss.
  • The parse() method assumes that the input date string is in UTC timezone. To parse dates in other timezones, you should use a library like moment.js.

Summary

The parse() method of the JavaScript Date object is used to parse a date string and returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC. The input date string should be in a format recognized by the Date.parse() method, and the method assumes that the input date string is in UTC timezone.

Published on: