Home > Web Front-end > JS Tutorial > body text

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

黄舟
Release: 2017-11-06 13:11:02
Original
2520 people have browsed it

Definition and Usage

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

Syntax

dateObject.getTimezoneOffset()
Copy after login

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>
Copy after login

Output:

-480
Copy after login

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>
Copy after login

Output:

The local time zone is: GMT -8
Copy after login

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>
Copy after login

This will produce the following results for India time zone:

getTimezoneOffset() : -330
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template