gpt4 book ai didi

iOS - 以国际格式和英语获取本地日期和时间

转载 作者:行者123 更新时间:2023-11-28 21:48:18 25 4
gpt4 key购买 nike

我正在尝试使用以下代码获取用户的本地日期和时间

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: @"dd MMM yyyy"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];

NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat: @"HH:mm"];
[timeFormatter setTimeZone:[NSTimeZone localTimeZone]];

self.appearedOnDate = [dateFormatter stringFromDate:[NSDate date]];
self.appearedOnTime = [timeFormatter stringFromDate:[NSDate date]];

它返回正确的值,但使用用户设备上设置的语言。例如,如果用户在他的设备上设置了简体中文,我得到的输出为

24 3月 2015 --- 22:13

我怎样才能得到英文的本地时间?

我猜这只发生在 iOS8 而不是 iOS6,但我不确定。

最佳答案

您可以在打印输出之前添加以下代码。

 [NSLocale availableLocaleIdentifiers];
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[timeFormatter setLocale:locale];

关于iOS - 以国际格式和英语获取本地日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29242918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com