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

Date.setTime() function in 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.

Using the setTime() function of the Date object you can set the time of the current date.

Syntax

Its Syntax is as follows

dateObj.setTime();

Example

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj1 = new Date('september 26, 89 5:4:25:96');
      var dateObj2 = new Date('November 1, 79 6:1:00:00');
      document.write("Current date: "+dateObj2.toUTCString());
      document.write("<br>");
      dateObj2.setTime(dateObj1.getTime());
      document.write("Modified date: "+dateObj2.toUTCString());
   </script>
</body>
</html>

Output

Current date: Thu, 01 Nov 1979 00:31:00 GMT
Modified date: Mon, 25 Sep 1989 23:34:25 GMT