-2

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?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Google `NSDateFormatter`. –  Jun 03 '13 at 05:12
  • Furthermore, this is not an ISO8601 string – borrrden Jun 03 '13 at 05:26
  • thanks h2c03! Sometimes I just need to know where to look. pardon the ignorance and thanks for the help. And sorry this isn't a ISO8601 formatted timestamp string? – user2159691 Jun 03 '13 at 05:48
  • ok so from what I've read, there's no way to turn this 2013-05-11 05:54:07.589698 into this 05:54:07 without manipulating the string first two separate the two. Is this correct? – user2159691 Jun 03 '13 at 06:47

1 Answers1

2

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);
Anupdas
  • 10,211
  • 2
  • 35
  • 60