gpt4 book ai didi

iphone - 在iPhone上使用NSDateFormatter,保护区域设置但丢失日期组件

转载 作者:行者123 更新时间:2023-12-03 18:26:25 25 4
gpt4 key购买 nike

这是一个有点棘手的问题。我在 iPhone 上使用 NSDateFormatter,但我只想显示标准日期,而不显示年份部分。但保留用户日期的区域设置格式。

我可以使用

轻松覆盖格式
[dateFormatter setDateFormat:@"h:mma EEEE MMMM d"];  // hurl.ws/43p9 (date formatting)

但现在日期是我的 en-nz 格式,例如 7 月 7 日星期三中午 12:01。所以我已经完全删除了世界各地任何其他用户的语言环境。

我想说。

Give me the correct localized date for this users region but omit the years component.

由于日期显示为字符串,因此我很想直接从日期中删除年份部分,然后通过将其从字符串中删除来删除年份部分。

最佳答案

从 iOS 4.0 开始,支持开箱即用的不同区域设置变体的正确方法是使用上面提到的以下 API(请参阅 WWDC 2012 的本地化 session ):

+dateFormatFromTemplate:options:locale:

例如,要获取不带年份的长日期格式:

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];  
NSString *longFormatWithoutYear = [NSDateFormatter dateFormatFromTemplate:@"MMMM d" options:0 locale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:longFormatWithoutYear];
//format your date...
//output will change according to locale. E.g. "July 9" in US or "9 de julho" in Portuguese

关于iphone - 在iPhone上使用NSDateFormatter,保护区域设置但丢失日期组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1101631/

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