HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – NSDateFormatter不为“z”或“zzz”说明符显示“Asia / Kolkata”的时区缩写,只显示GMT偏移量大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在iOS5模拟器和设备上,NSDateFormatter不为“z”或“zzz”说明符显示“Asia / Kolkata”的时区缩写.
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Kolkata"];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"z"; // or @"zzz"
dateFormatter.timeZone = timeZone;

NSLog(@"date String: %@",[dateFormatter StringFromDate:[NSDate date]]); // "GMT+05:30",expected "IST"
NSLog(@"time zone abbreviation: %@",[timeZone abbreviationForDate:[NSDate date]]); // "IST"

我希望上面的代码输出

IST
IST

但它输出

GMT+05:30
IST

编辑

将语言环境设置为印度语语言环境似乎没有帮助.

NSLocale *indianEnglishLocale = [[[NSLocale alloc] initWithLocalEIDentifier:@"en_IN"] autorelease];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Kolkata"];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:indianEnglishLocale];
[dateFormatter setDateFormat:@"z"]; // or @"zzz"
[dateFormatter setTimeZone:timeZone];

NSLog(@"date String: %@",[timeZone abbreviationForDate:[NSDate date]]); // "IST"

我希望上面的代码输出

IST
IST

但它输出

GMT+05:30
IST

这是一个错误吗?难道我做错了什么? People have mentioned that NSDateFormatter has bugs,especially when a time zone is specified in the format string. Could this be one of those bugs?

解决方法

http://www.cocoabuilder.com/archive/cocoa/310977-nsdateformatter-not-working-on-ios-5.html#311281

http://www.cocoabuilder.com/archive/cocoa/313301-nsdateformatter-not-working-on-ios-5.html#313301开始

– -编辑 – –

根据formats及其rules的unicode文档,V格式可能是更好的选择:

就我而言,对于以下代码

NSLocale *indianEnglishLocale = [[[NSLocale alloc] initWithLocalEIDentifier:@"en_IN"] autorelease];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Kolkata"];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:indianEnglishLocale];
[dateFormatter setDateFormat:@"V"];
[dateFormatter setTimeZone:timeZone];

NSLog(@"V date String: %@",[dateFormatter StringFromDate:[NSDate date]]);

我收到以下输出

V date String: IST

大佬总结

以上是大佬教程为你收集整理的ios – NSDateFormatter不为“z”或“zzz”说明符显示“Asia / Kolkata”的时区缩写,只显示GMT偏移量全部内容,希望文章能够帮你解决ios – NSDateFormatter不为“z”或“zzz”说明符显示“Asia / Kolkata”的时区缩写,只显示GMT偏移量所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。