Blog from Wen: Convert local time to GMT time

Source: Internet
Author: User
Tags time zones

When writing the RSS feed interface, it is found that the RSS time (GMT time) of the final output article, the time displayed on the local and the time displayed on the server is inconsistent. The reason is that time zones are inconsistent, so how do you convert time to a uniform time in JavaScript?

1. View the local and server time zones

With the date command, you know:

    • The local time zone is: gmt+0800 (CST)
    • The time zone for the server is: gmt+0400 (MSK)

First, it is important to be clear that the time on the article is based on local time, that is, gmt+0800 (CST).

2. Code debugging

Easy to understand, let's raise a chestnut, for example, the time of the article is 2013-11-05 00:00:00, then the time we finally want is 2013-11-04 16:00:00 GMT

On this machine

var date = new Date(‘2013-11-05 00:00:00‘); // Tue Nov 05 2013 00:00:00 GMT+0800 (CST)date.toGMTString(); // Mon, 04 Nov 2013 16:00:00 GMT

On the server:

var date = new Date(‘2013-11-05 00:00:00‘); // Tue Nov 05 2013 00:00:00 GMT+0400 (MSK)date.toGMTString(); // Mon, 04 Nov 2013 20:00:00 GMT

By contrast, it is also possible to see that the time required to convert on the server was local time, resulting in inconsistent GMT time for the final conversion.

3. How to Resolve

The Date object has the getTimezoneOffset () method, which returns the number of minutes between the local time and the GMT or UTC time.

var date = new Date(‘2013-11-05 00:00:00‘); // Tue Nov 05 2013 00:00:00 GMT+0400 (MSK)var localTime = date.getTime() - 8 * 3600000 - date.getTimezoneOffset() * 60000; // 先将文章的时间转换为服务器的本地时间 var newDate = new Date(localTime); // Mon Nov 04 2013 20:00:00 GMT+0400 (MSK)newDate.toGMTString(); // Mon, 04 Nov 2013 16:00:00 GMT

As you can see, we also get the results we want on the server.

4. Final results
 function  Getgmtstring (time, Localtimezone) {var date = new Date (time); return new Date (Date.gettime ()-localtime *  3600000-date.gettimezoneoffset () * 60000). toGMTString (); }            



// Switch to Beijing time zone format     settimeend (date) {        = Date.gettime ()-8 * 3600000-date.gettimezoneoffset () * 60000;          This . Props.setendtimeios (localtime);         // this.props.BJTime (date);        Console.log ("465454" + localtime);    }

The Setendtimeios method in action saves the time format that has been converted to the Beijing time zone in GetState () for ease of use:

/** *function  setendtimeios (date    ) {= date;     return {        type:set_end_time,        data: {endtimes}     }}

Finally, through the time component, the default time and the selected time, respectively, passed in:

return(dispatch,getstate) ={Let date=NewDate ();        Let Defaultdate; Let year=date.getfullyear (); Let month= Date.getmonth () + 1; Let Day=date.getdate (); Let hour=date.gethours (); Let minute=date.getminutes (); Let second=date.getseconds (); Month= Month < 10? "0" +Month:month; Day= Day < 10? "0" +Day:day; Hour= Hour < 10? "0" +Hour:hour; Minute= Minute < 10? "0" +Minute:minute; Second= Second < 10? "0" +Second:second; //default time    defaultdate= year + "-" + month + "-" + Day + "" + Hour + ":" + Minute + ":" +second; Let advertisement=advertisementlist; Let str=json.stringify (advertisement); Let Appconfigversionid= ' Iostestconfigversion '; Let AppId= ' Jia-jian-rest '; Let Appconfigentity= {}; Let Stateone=getState (). ApplicationState; Let Statetwo=getState (). Iosandroidstate; //Select the time after let datestart = Statetwo.starttimes?stateTwo.startTimes:defaultDate; Let dateend = Statetwo.endtimes?stateTwo.endTimes:defaultDate;Appconfigentity.configentityjson=str; Appconfigentity.configdescription=statetwo.configdescription; Appconfigentity.starttime= DateStart; Appconfigentity.endtime= dateend; Appconfigentity.appconfigversionid=Appconfigversionid; Api.addiosimglist (Appconfigentity,appid,params={Dispatch (Iosimgcongsetter (params));    }); }

Blog from Wen: Convert local time to GMT time

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.