Store the original time zone in NSDateFormatter.

I have strings like 2011-01-19T20: 30: 00-5: 00 and I would like to parse them in NSDate, but I want to keep the original time zone.

NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat: @"yyyy-MM-dd'T'HH:mm:ssZ"];
NSLog(@"%@", [dateFormatter dateFromString: dateString]);
[dateFormatter release];

This snippet gives me 2011-01-20 02:30:00 +0100 , which is also correct, but I want to keep the original time zone -0500 instead of my local time zone +0100 in NSDate.

+3
source share
1 answer

First of all, you should be aware that NSDate objects do not store anything related to their locales or time zones, and internally they are essentially represented as a few seconds from the first moment of January 1, 2001 GMT.

NSTimezone, dateFromString:

[dateFormatter setTimeZone:timezoneForString];

.

NSTimezone, , , "2011-01-19T20: 30: 00-5: 00", NSTimezone -5: 00, (, "/-" ) (, " " ) UTC. , , .

0

Source: https://habr.com/ru/post/1784785/


All Articles