当我在 PST 下面运行此代码时, AKST 时区永远不会运行...这两个都完美运行:
NSTimeZone *pacificTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"PST"];
NSTimeZone *alaskaTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"AKST"];
但这些都没有运行:
}else if ([local isEqual: pacificTimeZone]) {
NSLog(@"Pacific");
} else if ([local isEqual: alaskaTimeZone]) {
NSLog(@"Alaska");
有什么建议吗?
NSTimeZone *local = [NSTimeZone localTimeZone];
NSTimeZone *centralTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"CST"];
NSTimeZone *easternTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];
NSTimeZone *mountainTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"MST"];
NSTimeZone *pacificTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"PST"];
NSTimeZone *alaskaTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"AKST"];
NSTimeZone *honoluluTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"HST"];
NSLog(@"LOCAL TIME ZONE: %@", local);
NSLog(@"CST TIME %@", centralTimeZone);
NSLog(@"EST TIME %@", easternTimeZone);
NSLog(@"MST TIME %@", mountainTimeZone);
NSLog(@"PST TIME %@", pacificTimeZone);
NSLog(@"AKST TIME %@", alaskaTimeZone);
NSLog(@"HST TIME %@", honoluluTimeZone);
if ([local isEqual: centralTimeZone])
{
NSLog(@"Central");
} else if ([local isEqual: easternTimeZone]) {
NSLog(@"Eastern");
} else if ([local isEqual: mountainTimeZone]) {
NSLog(@"Mountain");
}else if ([local isEqual: pacificTimeZone]) {
NSLog(@"Pacific");
} else if ([local isEqual: alaskaTimeZone]) {
NSLog(@"Alaska");
} else if ([local isEqual: honoluluTimeZone]) {
NSLog(@"Honolulu");
}
谢谢!
答案 0 :(得分:2)
通常,不鼓励使用缩写,但“UTC”或“GMT”等唯一实例除外。时区缩写不是标准化的,因此给定的缩写可能有多种含义 - 例如,“EST”指的是美国和澳大利亚的东部时间
相反,请使用timeZoneWithName
。支持的时区来自IANA TZ database,您可以找到它们的列表here。
美国最常见的区域如下:
Alaska = "America/Anchorage"
Hawaii = "Pacific/Honolulu"
Pacific = "America/Los_Angeles"
Arizona = "America/Phoenix"
Mountain = "America/Denver"
Central = "America/Chicago"
Eastern = "America/New_York"