It is necessary to send / receive dates via API in json format. How to handle and store temporary zones?
I noticed that dates from NSString to NSDate with time zones are automatically converted to UTC.
// date with time zone NSString *dateString = @"2016-11-08T07:00:00.000+11:00"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"; dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"CDT"]; NSLog(@"1. CDT date = %@", [dateFormatter dateFromString:dateString]); dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; NSLog(@"2. UTC date = %@", [dateFormatter dateFromString:dateString]); dateFormatter.timeZone = [NSTimeZone systemTimeZone]; NSLog(@"3. system date = %@", [dateFormatter dateFromString:dateString]); // date without time zone dateString = @"2016-11-08"; dateFormatter.dateFormat = @"yyyy-MM-dd"; dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"CDT"]; NSLog(@"4. CDT date = %@", [dateFormatter dateFromString:dateString]); dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; NSLog(@"5. UTC date = %@", [dateFormatter dateFromString:dateString]); dateFormatter.timeZone = [NSTimeZone systemTimeZone]; NSLog(@"6. system date = %@", [dateFormatter dateFromString:dateString]); Log:
1. CDT date = 2016-11-07 20:00:00 +0000 2. UTC date = 2016-11-07 20:00:00 +0000 3. system date = 2016-11-07 20:00:00 +0000 4. CDT date = 2016-11-08 06:00:00 +0000 5. UTC date = 2016-11-08 00:00:00 +0000 6. system date = 2016-11-07 21:00:00 +0000 This means that in order to correctly process the date, you need to define a time zone on the line and store the NSTimeZone time zone next to the NSDate date?
NSString *dateString = @"2016-11-08T07:00:00.000+03:00"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"; NSDateFormatter *dateFormatterForPrint = [[NSDateFormatter alloc] init]; dateFormatterForPrint.timeZone = [self timeZoneFromDateString:dateString]; dateFormatterForPrint.dateFormat = @"dd.MM.yyyy HH:mm:ss"; NSDate *date = [dateFormatter dateFromString:dateString]; NSLog(@"date is: %@", [dateFormatterForPrint stringFromDate:date]); Log:
date is: 08.11.2016 07:00:00
NSDatecompletely untied from the timezone, it means the moment of time, not the date / time. That is, when parsing, the poem will have to be dumped into two fields - the timezone and timestamp. You can send it in a specific timezone with its indication in the string. To do this, set thetimeZoneformatter and usestringFromDate:The normalNSDategoes to the entrance, well, just prepare it so that it shows the right time in the right time zone. And the server again takes your line and subtracts the timezone and timestamp separately from it - markov