IOS is always 8 hours slower than real time

Source: Internet
Author: User
Tags add time

For example, a Beijing-GMT "2014-4-4 22:00" (string) needs to be converted to nsdate. The conversion of strings into nsdate is generally done through nsdateformatter. On IOS, nsdate is stored in GMT time, so NSDateFormatter automatically handles the local time of the current time zone of the string, converting to GMT (the string "2014-4-4 22:00") to the UTC time ("2014-4-4 14:00" )。 If the NSDate (the number of seconds or milliseconds since longlong,1970) is passed to the server directly, the server will use it as a time of day (in fact it is GMT), which results in a 8-hour lag.

The right thing to do is to add time difference to this nsdate. The calculation of the jet lag requires knowing the current time zone. [Nstimezonesystemtimezone] can get the current time zone (East 8) and then use the Secondsfromgmtfordate: method to get the time difference (in seconds) for this timezone (East 8). The code is as follows:

nsdateformatter* df=[nsdateformatter new];//        [Dfsetlocale:[nslocale  Currentlocale]];       [Email protected] "Yyyy-mm-dd hh:mm";        nsdate* date=[dfdatefromstring:@ "2014-4-4 22:00"];        Nstimezone *zone =[nstimezone Systemtimezone];        Nsinteger interval = [zonesecondsfromgmtfordate:date];        NSDate *localedate =[date  datebyaddingtimeinterval:interval];       NSLog (@ "%@", localedate);

IOS is always 8 hours slower than real 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.