Home  >  Article  >  Web Front-end  >  JavaScript returns the time difference in minutes between Greenwich Mean Time and local time using the method getTimezoneOffset()

JavaScript returns the time difference in minutes between Greenwich Mean Time and local time using the method getTimezoneOffset()

黄舟
黄舟Original
2017-11-06 13:11:022450browse

Definition and Usage

The getTimezoneOffset() method returns the time difference between Greenwich Time and local time, in minutes.

Syntax

dateObject.getTimezoneOffset()

Return value

The time difference between local time and GMT time, in minutes.

Description

The getTimezoneOffset() method returns the number of minutes difference between local time and GMT time or UTC time. Essentially, this function tells us the time zone in which the JavaScript code is running, and whether the specified time is in daylight saving time.

The reason why the return is in minutes instead of hours is that some countries have time zones that are even less than an hour apart.

Tips and Comments:

Comment: Due to the convention of using daylight saving time, the return value of this method is not a constant.

Note: This method is always used in conjunction with a Date object.

Example

Example 1

In the following example, we will get the time difference in minutes between GMT time and local time:

<script type="text/javascript">

var d = new Date()
document.write(d.getTimezoneOffset())

</script>

Output:

-480

Example 2

Now we will convert the above example to GMT +/- hours:

<script type="text/javascript">

var d = new Date()
var gmtHours = d.getTimezoneOffset()/60
document.write("The local time zone is: GMT " + gmtHours)

</script>

Output:

The local time zone is: GMT -8

javascript date. The getTimezoneOffset() method returns the time zone offset in minutes for the current zone. The time zone offset is the difference in minutes from Greenwich Mean Time (GMT) relative to your local time.

For example, if the time zone is GMT+10, -600 will be returned. Daylight saving time prevents this value from being constant.

Example:

<html>
<head>
<title>JavaScript getTimezoneOffset Method</title>
</head>
<body>
<script type="text/javascript">
  var dt = new Date();
  var tz = dt.getTimezoneOffset(); 
  document.write("getTimezoneOffset() : " + tz ); 
</script>
</body>
</html>

This will produce the following results for India time zone:

getTimezoneOffset() : -330

The above is the detailed content of JavaScript returns the time difference in minutes between Greenwich Mean Time and local time using the method getTimezoneOffset(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn