Romi Romi - 5 months ago 17
iOS Question

How to parse datetime in Objective C

My input date is in the following format, how can I parse it using Objective C?

"created_at":"2015-09-03T12:56:37.922Z"
"created_at":"2015-09-04T07:08:20.349Z"
"created_at":"2015-09-03T09:22:33.058Z"

Answer

try this

NSString *getDate=@"2015-09-03T12:56:37.922Z";

NSDateFormatter* dateformat = [[NSDateFormatter alloc]init];
[dateformat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];

// convert NSstring to NSDate
NSDate* date = [dateformat dateFromString: getDate];
NSLog(@"date: %@",date);

// convert NSDate to NSString
NSString *newString = [dateformat stringFromDate:date];
NSLog(@"newString: %@", newString);

[dateformat setDateFormat:@"yyyy-MM-dd HH:mm a"];

   // convert NSDate to NSString
NSString *finalString = [dateformat stringFromDate:date];
NSLog(@"final String: %@", finalString);  
Comments