Question

I have an ISO8601 string

2013-05-11 05:54:07.589698

Is there anyway to extract just the time from this without string manipulation?

Was it helpful?

Solution

There are many answers for similar questions, just posting this answer incase you haven't found it yet

NSString *inputString = @"2013-05-11 05:54:07.589698";

NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSSSSS"];

NSDate *date = [dateFormatter dateFromString:inputString];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSString *timeString = [dateFormatter stringFromDate:date];

NSLog(@"%@",timeString);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top