如何获取完整的时区名称 ios
Posted
技术标签:
【中文标题】如何获取完整的时区名称 ios【英文标题】:How to get full time zone name ios 【发布时间】:2015-09-29 01:58:20 【问题描述】:我们如何在 ios 中获得完整的时区名称 像印度标准时间而不是 IST。
NSTimezone *timeZone=[NSTimeZone localTimeZone];
我得到的是 GMT,但我希望它像完整形式的字符串
【问题讨论】:
【参考方案1】:您可以从name
属性中获取名称:
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSLog(@"Name: %@", timeZone.name);
结果:
2015-07-10 13:31:46.292 APP[2904:50429] Name : Asia/Calcutta
您可以使用- localizedName:locale:
获得具有所需样式NSTimeZoneNameStyle
的名称
NSTimeZone* timeZone = [NSTimeZone timeZoneWithAbbreviation:@"IST"];
NSString* timeZoneName = [timeZone localizedName:NSTimeZoneNameStyleStandard locale:[NSLocale currentLocale]];
NSLog(@"Name : %@", timeZoneName);
结果:
2015-07-10 13:36:31.771 APP[2940:51197] Name : India Standard Time
如果您致电[NSTimeZone abbreviationDictionary]
,您将获得一本包含所有可用缩写词的字典。
NSLog(@"%@",[NSTimeZone abbreviationDictionary]);
结果:
2015-07-10 11:44:05.954 APP[1472:26120]
ADT = "America/Halifax";
AKDT = "America/Juneau";
AKST = "America/Juneau";
ART = "America/Argentina/Buenos_Aires";
AST = "America/Halifax";
BDT = "Asia/Dhaka";
BRST = "America/Sao_Paulo";
BRT = "America/Sao_Paulo";
BST = "Europe/London";
CAT = "Africa/Harare";
CDT = "America/Chicago";
CEST = "Europe/Paris";
CET = "Europe/Paris";
CLST = "America/Santiago";
CLT = "America/Santiago";
COT = "America/Bogota";
CST = "America/Chicago";
EAT = "Africa/Addis_Ababa";
EDT = "America/New_York";
EEST = "Europe/Istanbul";
EET = "Europe/Istanbul";
EST = "America/New_York";
GMT = GMT;
GST = "Asia/Dubai";
HKT = "Asia/Hong_Kong";
HST = "Pacific/Honolulu";
ICT = "Asia/Bangkok";
IRST = "Asia/Tehran";
IST = "Asia/Calcutta";
JST = "Asia/Tokyo";
KST = "Asia/Seoul";
MDT = "America/Denver";
MSD = "Europe/Moscow";
MSK = "Europe/Moscow";
MST = "America/Denver";
NZDT = "Pacific/Auckland";
NZST = "Pacific/Auckland";
PDT = "America/Los_Angeles";
PET = "America/Lima";
PHT = "Asia/Manila";
PKT = "Asia/Karachi";
PST = "America/Los_Angeles";
SGT = "Asia/Singapore";
UTC = UTC;
WAT = "Africa/Lagos";
WEST = "Europe/Lisbon";
WET = "Europe/Lisbon";
WIT = "Asia/Jakarta";
【讨论】:
但我需要像印度标准时间而不是 IST,我的意思是全名谢谢 谢谢,但我的顾虑是一样的,我需要像我们在 windows 上那样的全名 在这种情况下,您需要使用- localizedName:locale:
获取具有所需样式的名称- localizedName:locale:
哦,太好了,我不知道你可以得到这样的完整本地化时区名称。【参考方案2】:
我想你正在寻找这个
NSDictionary *tempDict = [NSTimeZone abbreviationDictionary];
NSTimeZone* timeZone;
NSString* timeZoneName;
for(NSString *str in tempDict)
timeZone = [NSTimeZone timeZoneWithAbbreviation:str];
timeZoneName = [timeZone localizedName:NSTimeZoneNameStyleStandard locale:[NSLocale currentLocale]];
NSLog(@"%@ --- %@", timeZoneName,timeZone);
输出会像...
东部标准时间 --- 美国/纽约 (EDT) 偏移量 -14400(日光) 大西洋标准时间 --- 美国/哈利法克斯 (ADT) 偏移量 -10800(日光)
【讨论】:
【参考方案3】:试试这个;
NSTimeZone* timeZone = [NSTimeZone localTimeZone];
NSString* timeZoneName = [timeZone localizedName:NSTimeZoneNameStyleStandard locale:[NSLocale currentLocale]];
NSLog(@"timeZoneName : %@", timeZoneName);
【讨论】:
以上是关于如何获取完整的时区名称 ios的主要内容,如果未能解决你的问题,请参考以下文章