2013-08-27 155 views
0

Plz帮助我使用日期格式化程序:我需要根据选择的语言更改日期格式,并选择格式化时间24小时/ 12小时,因此动态更新它的主要麻烦。现在我做到了像动态日期格式取决于语言和语言环境

 [form setDateFormat:[NSString stringWithFormat:@"yyyy-MM-dd HH:mm"]]; 
     NSDate *normalDate = [form dateFromString:time]; 

     if (normalDate == nil) { 
      [form setDateFormat:[NSString stringWithFormat:@"yyyy-MM-dd h:mm a"]]; 
      normalDate = [form dateFromString:time]; 
     } 

     [form setDateFormat:[NSString stringWithFormat:@"MMMM dd, yyyy %@",timeF]]; 
     normalStr = [form stringFromDate:normalDate]; 

但它是非常不好的方法,我认为,请帮助我做出更好的系统,这也将工作)

回答

0

我从来没有尝试过,但你可以尝试使用Localizable.strings文件。 所以,当你定位你的应用程序,在Localizable.strings(英文),你可以有

"date_format" = "yyyy-MM-dd HH:mm"; 

,而也许在Localizable.strings(OtherLanguage)你把

"date_format" = "yyyy-MM-dd h:mm a"; 

,并在您DateFormatter你做

[form setDateFormat:NSLocalizedString(@"date_format", nil)] 
+0

我想到了这个,但麻烦,用户可以设置例如英语和12/24小时格式,所以每种语言有2个位置) – gronzzz

0

不知道你真正想做的事情)

考虑使用NSDateFormatter dateFormatFromTemplate:options:locale:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/clm/NSDateFormatter/dateFormatFromTemplate:options:locale: 与当前区域的值([NSLocale curentLocale]) 或用[dateFormatter的setLocale:...],并指定所选语言

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; 

和[dateFormatter setTimeStyle:...]将它设置为NSDateFormatterShortStyle,NSDateFormatterMediumStyle,或NSDateFormatterLongStyle

相关问题