2010-05-19 35 views
4

我知道,如果想顯示的日期,即Objective-C中有方法可以將日期轉換爲字符串以當前區域設置。持續時間是否等同?我的意思是像00:12:34(0小時,12分34秒)在不同的地區?本地化持續時間爲NSString。 (可可)

+0

可能重複[如何分析目標C中的ISO-8601持續時間?(http://stackoverflow.com/questions/1146416/how-to-parse-an-iso-8601-duration-in -objective-c) – 2011-08-23 05:03:42

回答

0

我想你需要的是創建一個NSDateFormatter並調用setDateStyle:將其設置爲NSDateFormatterNoStyle,然後它將只顯示沒有顯示日期的時間。

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

謝謝,但這是時間,而不是持續時間。我的意思是像秒錶的時間。 – Frans 2010-05-20 14:22:51

+0

我也有興趣根據持續時間有多長時間來動態打印'1小時'或'20分鐘'或'2小時12分鐘'。 – miho 2012-08-13 18:52:05