The JavaScript time zone and the date () function are detailed

Source: Internet
Author: User
Tags local time month name

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

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:

The code is as follows Copy Code

<script type= "Text/javascript" >

var d = new Date ()
document.write (D.gettimezoneoffset ())

</script> output:


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

The code is as follows Copy Code

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

The code is as follows Copy Code
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


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:

The code is as follows Copy Code

New Date ("Month dd,yyyy hh:mm:ss");
New Date ("Month dd,yyyy");
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:

The code is as follows Copy Code

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