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