Wednesday, May 8, 2013

JSON Date UTC Formatting for iOS to Web API Communication

Noob, but I have an iOS app POSTing data to a .NET Web API service and I want to keep the date/time data in UTC, so here's what I'm doing when I serialize my JSON.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; // Keeps time in UTC.
NSDate *myDate = [NSDate date];
NSString *myDateString = [dateFormatter stringFromDate:myDate];