A server is sending me dates in the form 2012-04-30T10:00:00 (as a string) which are used to add an entry to the Calendar. I’m using the following code to convert the date from a string to an NSDate which is then used to set the start date of the calendar event.
The entries for both 2012-04-30T10:00:00 and 2012-04-30T10:00:00Z are being added at the same time within the calendar. What should I do so that if the Z is present they get added as UTC times, or if absent they get added as local times?
+ (NSDate*) convertDate: (NSString*) fromString
{
[NSTimeZone resetSystemTimeZone];
NSLocale *enUSPOSIXLocale;
NSDateFormatter *sRFC3339DateFormatter = [[NSDateFormatter alloc] init];
enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[sRFC3339DateFormatter setLocale:enUSPOSIXLocale];
[sRFC3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[sRFC3339DateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date = [sRFC3339DateFormatter dateFromString:fromString];
return date;
}
Set the timezone on your formatter to UTC. This will do the trick:
Edit: You’ll need to set the timezone to UTC if the Z is present, otherwise use the system timezone, like: