Localized duration as NSString. (Cocoa)

I know if I want to show the date that Objective-C has methods for converting the date as a string into the current locale settings. Is there an equivalent duration? I mean something like 00:12:34 (0 hours, 12 minutes and 34 seconds) in different places?

+4
source share
1 answer

I think you need to create an NSDateFormatter and call setDateStyle: set it to NSDateFormatterNoStyle, and then only display the time without displaying the 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]; 
0
source

Source: https://habr.com/ru/post/1310253/


All Articles