Introduction to javascript time zone functions

Source: Internet
Author: User
Tags time zones

Js time zone functions:

Set datename to a created Date object

================================
Datename. getTimezoneOffset ()
-- Get the difference between the local time and GMT time (Greenwich Mean Time). The return value is in minutes.
================================

Example: Obtain the GMT time and any time zone based on the local time

D = new Date (); // create a Date object
LocalTime = d. getTime ();
LocalOffset = d. getTimezoneOffset () * 60000; // obtain the number of milliseconds for the local time offset
Utc = localTime + localOffset; // utc is the GMT time
Offset = 10; // take the Hawaiian time as an example.
Hawaii = utc + (3600000 * offset );
Nd = new Date (hawaii );
Document. writeln ("Hawaii time is" + nd. toLocaleString () + <br> ");

======================================
Date. UTC (year, month, day, hours, minutes, seconds, MS)

-- Construct a Date object in GMT and return a Date object
====================================

If you directly create a Date object, the input is the local time, and the object is created using Date. UTC, the input is the GMT time.

======================================
Datename. toUTCString ()
Datename. toGMTString ()
-- Output GMT time
======================================

The two functions are the same. The latter is considered outdated.

This function outputs The GMT time relative to the local time output by toLocaleString.

======================================
Others
======================================

GetUTCDate () function -- returns the Day (1-31) of the month expressed in the World Standard Time (UTC) in the date object)
GetUTCDay () function -- returns the day of the week (0-6) represented by the World Standard Time (UTC) in the date object)
GetUTCFullYear () function -- returns the four-digit year represented by the World Standard Time (UTC) in the date object.
GetUTCHours () function -- returns the number of hours (0-23) represented by the World Standard Time (UTC) in the date object)
GetUTCMilliseconds () function -- returns the number of milliseconds (0-999) represented in the World Standard Time (UTC) in the date object)
GetUTCMinutes () function -- returns the number of minutes (0-59) represented by the World Standard Time (UTC) in the date object)
GetUTCMonth () function -- returns the number of months (0-11) represented in the World Standard Time (UTC) in the date object)
GetUTCSeconds () function -- returns the number of seconds (0-59) represented by the World Standard Time (UTC) in the date object)

SetUTCDate () function -- sets the day of the month expressed in the World Standard Time (UTC) in the date object, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, January 1, 1970)
SetUTCFullYear () function -- sets the year, month, and day represented by the World Standard Time (UTC) in the date object, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, January 1, 1970)
SetUTCHours () function --- sets the hour, minute, second, and millisecond represented by the World Standard Time (UTC) in the date object, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, January 1, 1970)
SetUTCMilliseconds () function -- sets the number of milliseconds expressed in the World Standard Time (UTC) in the date object, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, January 1, 1970)
SetUTCMinutes () function -- sets the minute, second expressed in the World Standard Time (UTC) in the date object, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, January 1, 1970)
SetUTCMonth () function -- sets the month and day represented by the World Standard Time (UTC) in the date object, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, January 1, 1970)
SetUTCSeconds () function -- sets the second in milliseconds represented by the World Standard Time (UTC) in the date object, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, January 1, 1970)

In js, we generally talk about the time difference between Greenwich Mean Time and local time, in minutes. This is no different from php and asp. Next we will combine date () function to introduce issues related to the js time zone.

Definition and usage

The getTimezoneOffset () method returns the time difference between Greenwich Mean Time and local time, in minutes.
Syntax
DateObject. getTimezoneOffset () Return Value
The time difference between the local time and the GMT time, in minutes.
In the following example, we will obtain the time difference between the GMT time and the local time in minutes:

Copy codeThe Code is as follows: <script type = "text/javascript">
Var d = new Date ()
Document. write (d. getTimezoneOffset ())
</Script>

Output:
-480
Set datename to a created Date object
================================
Datename. getTimezoneOffset ()
-- Get the difference between the local time and GMT time (Greenwich Mean Time). The return value is in minutes.
================================

Example: Obtain the GMT time and any time zone based on the local timeCopy codeThe Code is as follows: d = new Date (); // create a Date object
LocalTime = d. getTime ();
LocalOffset = d. getTimezoneOffset () * 60000; // obtain the number of milliseconds for the local time offset
Utc = localTime + localOffset; // utc is the GMT time
Offset = 10; // take the Hawaiian time as an example.
Hawaii = utc + (3600000 * offset );
Nd = new Date (hawaii );
Document. writeln ("Hawaii time is" + nd. toLocaleString () + <br> ");
Date. UTC (year, month, day, hours, minutes, seconds, MS)
-- Construct a Date object in GMT and return a Date object

====================================
If you directly create a Date object, the input is the local time, and the object is created using Date. UTC, the input is the GMT time.
The Code is as follows:
Datename. toUTCString ()
Datename. toGMTString ()
-- Output GMT time
======================================
The two functions are the same. The latter is considered outdated.
This function outputs The GMT time relative to the local time output by toLocaleString.

Description
The getTimezoneOffset () method returns the number of minutes that differ between the local time and the GMT time or UTC time. In fact, this function tells us the time zone for running JavaScript code and whether the specified time is runtime.
The returned result is measured in minutes rather than hours, because the time zone occupied by some countries is less than an hour.
Tips and notes:
Note: the return value of this method is not a constant due to the usage of the delimiter.
Note: This method is always used in combination with a Date object.

Var myDate = new Date ()
The Date object automatically saves the current Date and time as its initial value.
The following five parameters are supported:Copy codeThe Code is as follows: new Date ("month dd, yyyy hh: mm: ss ");
New Date ("month dd, yyyy ");
New Date (yyyy, mth, dd, hh, mm, ss );
New Date (yyyy, mth, dd );
New Date (MS );

Note that in the last form, the parameter represents the number of milliseconds between the creation time and the GMT time on January 1, January 1, 1970. The meanings of various functions are as follows:
Month: represents the name of a month in English, from January to December.
Mth: an integer representing the month, from (January 1, January) to 11 (January 1, December)
Dd: the day of a month, from 1 to 31.
Yyyy: The Year in four-digit format.
Hh: hours, from 0 (midnight) to 23)
Mm: The number of minutes, an integer from 0 to 59.
Ss: number of seconds, an integer from 0 to 59
Ms: Number of milliseconds, an integer greater than or equal to 0
For example:Copy codeThe Code is as follows: new Date ("January 22:19:35 ");
New Date ("January 12,2006 ");
New Date );
New Date (2007,0, 12 );
New Date (1137075575000 );

I didn't know before that js also had time zones. I thought it was only asp, and php. I didn't expect any time zones. If you need them, please refer to them.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.