आईओएस 5 सिम्युलेटर और डिवाइस पर, NSDateFormatter "एशिया/कोलकाता "ज़ेड" या "ज़ज़" विनिर्देशक के लिए।NSDateFormatter "z" या "zzz" विनिर्देशक के लिए "एशिया/कोलकाता" के लिए समय क्षेत्र संक्षेप नहीं दिखाता है, केवल जीएमटी ऑफसेट
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: %@", [dateFormatter stringFromDate:[NSDate date]]); // "GMT+05:30", expected "IST"
NSLog(@"time zone abbreviation: %@", [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?
ध्यान दें कि आधुनिक आईओएस में एक ही तरीका है एप्पल द्वारा की सिफारिश की एक तारीख उपयोग करने के लिए फॉर्मेटर है: https://stackoverflow.com/a/42370648/294884 – Fattie