Javascript time zone function introduction _ time date-js tutorial

Source: Internet
Author: User
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 js Time Zone issues 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 () +
");

======================================
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:

The Code is as follows:


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.