如何在iOS中获得TimeZone的三个字母缩写
[NSTimeZone abbreviationDictionary]
的缩写为3个字母的代码。例如:
NSZT
,PDT
,EST
等
但是
NSString * ss = [NSTimeZone timeZoneWithName:@"Pacific/Auckland"].abbreviation;
给出 GMT+12
。
是否可以代替 NSZT/NZDT
?
答案 0 :(得分:1)
您可以使用以下代码获取时区的完整标准本地化名称
目标c:
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Pacific/Auckland"];
NSString* timeZoneName = [timeZone localizedName:NSTimeZoneNameStyleStandard
locale:[NSLocale currentLocale]];
NSLog(@"%@", timeZoneName);
快捷键:
let timezone:TimeZone = TimeZone.init(identifier: "Pacific/Auckland") ?? TimeZone.current
print(timezone.localizedName(for: .generic, locale: .autoupdatingCurrent))
print(timezone.localizedName(for: .standard, locale: .autoupdatingCurrent))
输出:
目标c:
新西兰标准时间
快捷键:
可选(“新西兰标准时间”)
可选(“新西兰标准时间”)
我认为现在您可以通过拆分和组合字符串从新西兰标准时间获得NZST
目标c:
NSMutableString * firstCharacters = [NSMutableString string];
NSArray *wordsArray = [timeZoneName componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
for (NSString * word in wordsArray){
if ([word length] > 0){
NSString * firstLetter = [word substringToIndex:1];
[firstCharacters appendString:[firstLetter uppercaseString]];
}
}
NSLog(@"%@", firstCharacters);
快捷键:
let fullName = timezone.localizedName(for: .standard, locale: .autoupdatingCurrent) ?? ""
var result = ""
fullName.enumerateSubstrings(in: fullName.startIndex..<fullName.endIndex, options: .byWords) { (substring, _, _, _) in
if let substring = substring { result += substring.prefix(1) }
}
print(result)
输出:
NZST
答案 1 :(得分:0)
如rmaddy abbreviationDictionary的注释中所述,只有51个条目,但是如果仅使用abbreviationDictionary中的名称,则可以使用以下代码:
NSDictionary *dict = [NSTimeZone abbreviationDictionary];
NSArray *abbreviations = [dict allKeysForObject:@"Pacific/Auckland"];
if (abbreviations.count > 0) {
NSLog(@"%@", abbreviations.firstObject);
}