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

Date.getTimezoneOffset() 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 getTimezoneOffset() function of the Date object returns the time zone difference between current date and universal time in minutes.

Syntax

Its Syntax is as follows

dateObj.getTimezoneOffset();

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

Output

-330

Example

Since we have maximum 31 days in a month the date should be between 1 to 31 else this function returns NaN.

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

Output

NaN