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

Date.UTC() 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.

The Date.UTC() function accepts the details of the Date and treats them as UTC.

Syntax

Its syntax is as follows

dateObj.UTC(year, month[, day[, hour[, minute[, second[, millisecond]]]]]);

Example

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date(Date.UTC(89, 8, 26, 3, 4, 5));
      var date = dateObj.toUTCString();
      document.write(date);
   </script>
</body>
</html>

Output

Tue, 26 Sep 1989 03:04:05 GMT

Example

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var date = Date.UTC(2018, 10, 17);
      document.write("Number of milliseconds from 1970 to given date: " +date);
   </script>
</body>
</html>

Output

Number of milliseconds from 1970 to given date: 1542412800000