I know if want to display a date, that Objective-C has methods to convert the date as a string to the current locale settings. Is there an equivalent for durations? I mean something like 00:12:34 (0 hours, 12 minutes and 34 seconds) in different locales?
Asked
Active
Viewed 601 times
4
-
Possible duplicate [How to parse an ISO-8601 duration in Objective C?](http://stackoverflow.com/questions/1146416/how-to-parse-an-iso-8601-duration-in-objective-c) – Seb T. Aug 23 '11 at 05:03
1 Answers
0
I think what you need is to create an NSDateFormatter and call setDateStyle: to set it to NSDateFormatterNoStyle, and then it will only show the time without showing date.
NSString *originalString = @"01:23:45";
NSDateFormatter *fromFormatter = [[NSDateFormatter alloc] init];
[fromFormatter setDateFormat:@"HH:mm:ss"];
[fromFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[fromFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date = [fromFormatter dateFromString:originalString];
[fromFormatter release];
NSDateFormatter *toFormatter = [[NSDateFormatter alloc] init];
[toFormatter setDateStyle:NSDateFormatterNoStyle];
[toFormatter setTimeStyle:kCFDateFormatterFullStyle];
[toFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Tokyo"]];
[toFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"ja"] autorelease]];
NSString *s = [toFormatter stringFromDate:date];
NSLog(@"s:%@", s);
[toFormatter release];

zonble
- 4,213
- 1
- 21
- 14
-
Thank you, but this is for time, not for durations. I meant something like the time of a stopwatch. – Frans May 20 '10 at 14:22
-
I am also interested into printing a duration like '1hours' or '20minutes' or '2hr 12min' dynamically depending on how long the duration is. – miho Aug 13 '12 at 18:52