【问题标题】:Localized Duration as NSString. (Cocoa)本地化的持续时间为 NSString。 (可可)
【发布时间】:2011-02-21 10:52:27
【问题描述】:

我知道如果要显示日期,Objective-C 有方法将日期作为字符串转换为当前的语言环境设置。持续时间是否有等价物?我的意思是不同地区的 00:12:34(0 小时 12 分 34 秒)?

【问题讨论】:

标签: objective-c duration


【解决方案1】:

我想你需要的是创建一个 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];

【讨论】:

  • 谢谢,但这是时间,不是持续时间。我的意思是秒表的时间。
  • 我也有兴趣根据持续时间的长短动态打印“1小时”或“20分钟”或“2小时12分钟”等持续时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-12
  • 1970-01-01
  • 2012-05-05
  • 1970-01-01
相关资源
最近更新 更多