iphone - NSDate 关于日期时间显示

标签 iphone ios

这段代码。

NSDate *today = [NSDate date];  
NSArray *langs = [NSLocale preferredLanguages];
NSString *langID = [langs objectAtIndex:0];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:langID];
*format = [[NSDateFormatter alloc] init];
[format setLocale:locale];
[format setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
strTime = [[NSString alloc] initWithFormat:@"%@",[format stringFromDate:today]]; 

today is +0000.    
langID is en.    
but,strTime is Japan time.    

为什么? 我想根据语言设置显示日期和时间。

最佳答案

代替

NSArray *langs = [NSLocale preferredLanguages];
NSString *langID = [langs objectAtIndex:0];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:langID];

试试这个,

NSLocale *locale = [NSLocale currentLocale];

这应该有所帮助。

关于iphone - NSDate 关于日期时间显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13786772/

相关文章:

iphone - UILocalNotification 多次触发

iphone - 我的 iPad 应用程序需要更多内存

iphone - ios如何查看是否有来自Urban Airship的通知?

ios - Swift - 如何为 UITableView 制作自定义 header ?

iphone - 加载 NIB 时抛出 NSInternalConsistencyException

iphone - 一个 NIB 中的多个自定义 UITableViewCell - 如何在代码中引用?

iphone - _property 和 self.property 的区别

ios - 运行目的地[用户的] iPhone 对于运行该方案无效

objective-c - 无法从 iPhone 应用程序中的 URL 播放视频?

ios - 修复 Swift simd 库中的错误(以及缺少文档?)