JavaScript Date parse()

Posted by

Method

Introduction to the JavaScript Date parse() Method

The JavaScript Date parse() method is used to parse a string representing a date, and return the number of milliseconds since January 1, 1970, 00:00:00 UTC, with respect to the local time. It is important to note that this method does not necessarily convert the given string into a Date object, meaning that its behavior is different from the Date constructor.

Syntax of Date parse()

The syntax of the Date parse() method is as follows:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

Date.parse(dateString)

[/dm_code_snippet]

The Date parse() method takes a single parameter, which is a string representing the date to be parsed. This string should be in a format compatible with the Date.parse() method. Some examples of valid date strings are:

  • “Mon Jan 01 2018 00:00:00 GMT+0100 (CET)”
  • “December 25, 2017”
  • “2018-12-25T00:00:00+01:00”
  • “2018-12-25 00:00:00”

Examples of Date parse()

The following example illustrates how to use the Date parse() method:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var dateString = "Mon Jan 01 2018 00:00:00 GMT+0100 (CET)";

var millisecondsSinceEpoch = Date.parse(dateString);

console.log(millisecondsSinceEpoch); // 1514786800000

[/dm_code_snippet]

In the above example, we have used the Date parse() method to parse the date string “Mon Jan 01 2018 00:00:00 GMT+0100 (CET)”, and return the number of milliseconds since January 1, 1970, 00:00:00 UTC, with respect to the local time. As you can see, the result is 1514786800000.

Browser Support for Date parse()

The Date parse() method is supported in all major browsers, including Internet Explorer, Firefox, Chrome, Safari, and Opera.

Conclusion

The JavaScript Date parse() method is a useful tool for converting a string representing a date into a number of milliseconds since January 1, 1970, 00:00:00 UTC. This can be useful for working with dates in the JavaScript language. As mentioned previously, it is important to note that this method does not necessarily convert the given string into a Date object, meaning that its behavior is different from the Date constructor.