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?

有帮助吗?

解决方案

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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top