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

Date.getTime() function JavaScript


The Date object is a data type built into the JavaScript language. Date objects are created with the new Date( ) as shown below.

Once a Date object is created, a number of methods allow you to operate on it. Most methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object, using either local time or UTC (universal, or GMT) time.

JavaScript date getTime() method returns the numeric value corresponding to the time for the specified date according to universal time. The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00.

Syntax

Its syntax is as follows

dateObj.getTime();

Example

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date('september 26, 89 12:4:25:96');
   document.write("Time value: "+dateObj.getTime());
   </script>
</body>
</html>

Output

Time value: 622794865096

Example

In the same way if you haven’t passed anything while creating the date object this function returns the Current hour of the day.

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date();
      document.write("Time value: "+dateObj.getTime());
   </script>
</body>
</html>

Output

Time value: 1539764866888