Computer >> Computer tutorials >  >> Programming >> Javascript

What is the use of parsing dates in JavaScript?


Parsing a date will give us the time in milliseconds. Date.parse() is used to parse a date. This method actually returns a number of milliseconds between the provided date and January 1, 1970. It is programmed in that way by the developers to get time in milliseconds from January 1, 1970 to the date provided.

Example-1

In the following example, using Date.parse() a number of milliseconds were calculated between July 14, 2019 and January 1, 1970.

<html>
<body>
   <p>Date.parse() returns the number of milliseconds between
      july 14, 2019 and January 1, 1970 </p>
   <script>
      var time = Date.parse("july 14, 2019");
      document.write(time);
   </script>
</body>
</html>

Output

1563042600000

Example-2

In the following example, using Date.parse() a number of milliseconds were calculated between June 14, 1999 and January 1, 1970.

<html>
<body>
   <p>Date.parse() returns the number of milliseconds between
   the june 14, 1999 and January 1, 1970: </p>
   <script>
      var time = Date.parse("june 14, 1999");
      document.write(time);
   </script>
</body>
</html>

Output

929298600000