views:

64

answers:

1

Hi,

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?

A: 

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
Thank you, but this is for time, not for durations. I meant something like the time of a stopwatch.
Frans