Domanda

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?

È stato utile?

Soluzione

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top