JavaScript Time Zone Functions Introduction _ Date

Source: Internet
Author: User
Tags local time month name

JS Time zone function:

Set Datename to a Date object created

====================
Datename.gettimezoneoffset ()
--Gets the difference between local time and GMT time (GMT) and returns the value in minutes
====================

For example: Obtain GMT time and time of any time zone based on local time

D=new Date (); Create a Date Object
LocalTime = D.gettime ();
Localoffset=d.gettimezoneoffset () *60000; Number of milliseconds to get local time offset
UTC = LocalTime + localoffset; UTC is GMT time
offset = 10; Take the Hawaiian time as an example, East area 10
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)

--Constructs the date object in GMT time, returns the Date object
==============================

The Date object is created directly by thinking that the local time is entered, and that the object is created with Date.utc as GMT time

============================
Datename.toutcstring ()
Datename.togmtstring ()
--Output GMT time
============================

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

This function outputs GMT time relative to tolocalestring output local time

============================
Other
============================

getUTCDate () function--Returns the day of the month in World Standard Time (UTC) in a Date object (1-31)
getUTCDay () function--Returns the day of the week in World Standard Time (UTC) in a Date object (0-6)
getUTCFullYear () function--Returns the four-bit year in world standard Time (UTC) in a Date object
getUTCHours () function--Returns the number of hours in world standard Time (UTC) in a Date object (0-23)
getUTCMilliseconds () function--Returns the number of milliseconds (0-999) represented in world standard Time (UTC) in a Date object
getUTCMinutes () function--Returns the number of minutes in world standard Time (UTC) in a Date object (0-59)
getUTCMonth () function--Returns the number of months in world standard Time (UTC) in a Date object (0-11)
getUTCSeconds () function--Returns the number of seconds in world standard Time (UTC) in a Date object (0-59)


setUTCDate () function--sets the day of the month in World Standard Time (UTC) in the Date object, and returns the number of milliseconds (timestamp) between the Date object and the midnight of January 1, 1970
setUTCFullYear () function--sets the year, month, and day in the date object in world Standard Time (UTC) and returns the number of milliseconds (timestamp) between the Date object and midnight of January 1, 1970
The setUTCHours () function---Sets the hours, minutes, seconds, and milliseconds in the date object in world Standard Time (UTC), and returns the number of milliseconds (timestamp) between the Date object and the midnight of January 1, 1970
setUTCMilliseconds () function--sets the number of milliseconds in the date object in world Standard Time (UTC) and returns the number of milliseconds (timestamp) between the Date object and the midnight of January 1, 1970
setUTCMinutes () function--sets the minutes, seconds, in world Standard Time (UTC) in the Date object, and returns the number of milliseconds (timestamp) between the Date object and the midnight of January 1, 1970
setUTCMonth () function--sets the month, day, in world Standard Time (UTC) in the Date object, and returns the number of milliseconds (timestamp) between the Date object and the midnight of January 1, 1970
setUTCSeconds () function--sets the seconds, milliseconds, in world standard Time (UTC) in the Date object, and returns the number of milliseconds (timestamp) between the Date object and the midnight of January 1, 1970

In the time zone in JS we talk about the time difference between GMT and local times, in minutes, this is no different from php,asp. Below we combine the date () function to introduce the related problems of JS time zone.

Definitions and Usage

The getTimezoneOffset () method returns the difference in minutes between Greenwich and local time.
Grammar
Dateobject.gettimezoneoffset () return value
The time difference between local time and GMT times, in minutes.
In the following example, we will get the time difference between GMT and local time in minutes:

Copy Code code as follows:

<script type= "Text/javascript" >
var d = new Date ()
document.write (D.gettimezoneoffset ())
</script>

Output:
-480
Set Datename to a Date object created
====================
Datename.gettimezoneoffset ()
--Gets the difference between local time and GMT time (GMT) and returns the value in minutes
====================

For example: Obtain GMT time and time of any time zone based on local time
Copy Code code as follows:

D=new Date (); Create a Date Object
LocalTime = D.gettime ();
Localoffset=d.gettimezoneoffset () *60000; Number of milliseconds to get local time offset
UTC = LocalTime + localoffset; UTC is GMT time
offset = 10; Take the Hawaiian time as an example, East area 10
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)
--Constructs the date object in GMT time, returns the Date object

==============================
The Date object is created directly by thinking that the local time is entered, and that the object is created with Date.utc as GMT time
Code to copy the code as follows
Datename.toutcstring ()
Datename.togmtstring ()
--Output GMT time
============================
The two functions are the same. The latter is considered obsolete.
This function outputs GMT time relative to tolocalestring output local time

Description
The getTimezoneOffset () method returns the number of minutes between local time and GMT time or UTC time. In fact, the function tells us the time zone in which the JavaScript code is running, and whether daylight savings times are specified.
The return is in minutes, not in hours, because some countries occupy a time zone that is less than one hours apart.
Tips and Comments:
Note: The return value of this method is not a constant due to the practice of using daylight saving time.
Note: This method is always used in conjunction with a Date object.

var mydate=new Date ()
The Date object automatically saves the current date and time as its initial value.
The following 5 kinds of parameter form are:
Copy Code code 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 the last form, which represents the number of milliseconds to create and the difference between GMT time January 1, 1970. The meanings of the various functions are as follows:
Month: Month name in English, from January to December
MTH: Month by integer, from (January) to 11 (December)
DD: Represents the day ordinal of one months, from 1 to 31
YYYY: four-digit year
HH: Hours, from 0 (midnight) to 23 (11 o'clock in the evening)
MM: Number of minutes, integers from 0 to 59
SS: Number of seconds, integers from 0 to 59
MS: Number of milliseconds, integer greater than or equal to 0
Such as:
Copy Code code as follows:

New Date ("January 12,2006 22:19:35");
New Date ("January 12,2006");
New Date (2006,0,12,22,19,35);
New Date (2006,0,12);
New Date (1137075575000);

Did not know that JS also has time zone this always thought that only asp,php this kind of time zone is not expected to have, there is a need for students to refer to.

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.