Lee Probert Lee Probert - 16 days ago 7
iOS Question

Get NSDate object from server timestamp string

I have a server timestamp format like so

2016-11-21T11:04:02
, how can I convert this to an NSDate object?

Answer
NSString *serverTimeString=@"2016-11-21T11:04:02";

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate* date = [dateFormatter dateFromString:serverTimeString];