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

Date.getUTCFullYear() 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 getUTCFullYear() function of the Date object returns the full year of its current date according to the universal time.

Syntax

Its Syntax is as follows

dateObj.getUTCFullYear();

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(dateObj.getUTCFullYear());
   </script>
</body>
</html>

Output

1989

Example

Incase if you have created a date object without passing any value to its constructor this function returns the current year value according to the universal time.

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

Output

2018