JS Time difference Conversion gettimezoneoffset ()

Source: Internet
Author: User
Tags time zones local time

If our application is aimed at all over the world, when we want to show the time of the background (server) to the users in different regions, this time should be converted to the time of the user's client, and then presented to the user, that is, the following getTimezoneOffset ()

If you are simply converting local time to other time zones, use getTimezoneOffset ()

Concepts to understand: GMT

A place on the Greenwich Meridian, or Zero zone (in the time zone), is called Greenwich Mean Time, also known as the world. (More detailed concepts do not say, here we do not need.) For example, we China is East eight district, Beijing time is (gmt+08:00)

Get the time difference between local and GMT: New Date (). getTimezoneOffset (), in minutes.

Known GMT, conversion to local correct time

local time = GMT-Jetlag

Known local time, conversion corresponds to GMT:

GMT = local time + jet lag

Known local time, conversion time of other time zones

Because the time interval difference is in the hour unit. So calculate the time of 0 time zone, then subtract or add the corresponding hour (East N area will +n hours, West N area will N-hour). In order to facilitate the calculation, the East N area is a positive number, the West N area to do negative, namely:

Target timezone time = local time + slack + time zone interval

Example 1: Convert local current time to 0 time zone
//Calculate the time difference and convert to milliseconds:    varoffset =NewDate (). getTimezoneOffset () * 60 * 1000; //figure out the current time:    varNowdate =NewDate (). GetTime (); //figure out the corresponding green position time.    varGmtdate =NewDate (nowdate + offset);//Wed Apr 22:27:02 gmt+0800 (CST)    //convert cost to TIME format    vargmtdateinlocalstring = Gmtdate.tolocalestring ();//2016/4/20 pm 10:27:02

Example 2: Time to convert the local current time to the East 2 zone

    //East 2, East time zone, positive.    varZoneoffset = 2; //Calculate the time difference and convert to milliseconds:    varOffset2 =NewDate (). getTimezoneOffset () * 60 * 1000; //figure out the current time:    varNowDate2 =NewDate (). GetTime (); //at this time, the East 2 zone    varCurrentzonedate =NewDate (nowDate2 + offset2 + zoneoffset*60*60*1000); Console.log ("East 2 is now:" +currentzonedate);

JS Time difference Conversion gettimezoneoffset ()

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.