Trên trình mô phỏng và thiết bị iOS5, NSDateFormatter không hiển thị từ viết tắt múi giờ cho "Châu Á/Kolkata "cho" z "hoặc" zzz "specifier.NSDateFormatter không hiển thị chữ viết tắt múi giờ cho "Châu Á/Kolkata" cho thông số "z" hoặc "zzz", chỉ cần bù đắp GMT
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"
Tôi hy vọng các mã trên để đầu ra:
IST
IST
nhưng nó kết quả đầu ra:
GMT+05:30
IST
EDIT
Thiết lập miền địa phương đến một miền địa phương Ấn Độ không dường như giúp đỡ.
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"
Tôi hy vọng các mã trên để đầu ra:
IST
IST
nhưng nó kết quả đầu ra:
GMT+05:30
IST
Đây có phải là một lỗi? Tôi có làm điều gì sai? 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?
Do lưu ý rằng trong iOS hiện đại cách duy nhất khuyến cáo của Apple để sử dụng một ngày trình định dạng là: https://stackoverflow.com/a/42370648/294884 – Fattie