Nsdateformatter 以所选手机的语言返回日期。它可以一直只返回英文吗?

发布于 2024-12-17 16:10:11 字数 771 浏览 0 评论 0原文

我正在使用 dateformatter 来获取应用程序中的日期和时间。但是,当我更改手机日期格式器的语言时,我遇到了一个问题,它返回了手机所选语言的日期和时间,由于我们不支持多种语言,因此我的应用程序崩溃了。

请找到下面的代码片段:

NSDate *date=[NSDate date];
NSDateFormatter *objTimeFotmatter=[[NSDateFormatter alloc]init];
[objTimeFotmatter setDateFormat:@"HH:mm"];

NSDateFormatter *objDayFotmatter=[[NSDateFormatter alloc]init];
[objDayFotmatter setDateFormat:@"EEEE"];

NSString *objTime=[objTimeFotmatter stringFromDate:date];
NSString *objDay=[objDayFotmatter stringFromDate:date];

NSLog(@"objTime=%@",objTime);
NSLog(@"objDay=%@",objDay);

当我选择手机语言为古吉拉特语(印度)时,我使用 nslog 看到的输出如下,

2011-11-24 11:23:20.221 Belkin_Plugin[1110:707] objTime=૧૧:૨૩
2011-11-24 11:23:20.227 Belkin_Plugin[1110:707] objDay=ગુરુવાર

I am using dateformatter to get days and time in the my application. But I am facing an issue when I change the language of the phone dateformatter returns me day and time of the selected language of the phone due to which my app crashes as we are not supporting multiple languages.

Please find the below code snippet:

NSDate *date=[NSDate date];
NSDateFormatter *objTimeFotmatter=[[NSDateFormatter alloc]init];
[objTimeFotmatter setDateFormat:@"HH:mm"];

NSDateFormatter *objDayFotmatter=[[NSDateFormatter alloc]init];
[objDayFotmatter setDateFormat:@"EEEE"];

NSString *objTime=[objTimeFotmatter stringFromDate:date];
NSString *objDay=[objDayFotmatter stringFromDate:date];

NSLog(@"objTime=%@",objTime);
NSLog(@"objDay=%@",objDay);

When I selected the phone language as Gujrati (India) the output that I am seeing using the nslog using this is as follows,

2011-11-24 11:23:20.221 Belkin_Plugin[1110:707] objTime=૧૧:૨૩
2011-11-24 11:23:20.227 Belkin_Plugin[1110:707] objDay=ગુરુવાર

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离鸿 2024-12-24 16:10:11

将以下行添加到日期格式化程序中。

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocale];

对于您的情况,

NSDateFormatter *objTimeFotmatter=[[NSDateFormatter alloc]init];
[objTimeFotmatter setDateFormat:@"HH:mm"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[objTimeFotmatter setLocale:usLocale];

所有日期格式化程序都是如此。

Add the following lines to the dateformatter.

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocale];

In your case,

NSDateFormatter *objTimeFotmatter=[[NSDateFormatter alloc]init];
[objTimeFotmatter setDateFormat:@"HH:mm"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[objTimeFotmatter setLocale:usLocale];

Similarly for all dateformatters.

不弃不离 2024-12-24 16:10:11

斯威夫特

let dateFormatter = DateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale!
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let resultsDate = dateFormatter.string(from: date)

Swift

let dateFormatter = DateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale!
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let resultsDate = dateFormatter.string(from: date)
娇俏 2024-12-24 16:10:11

尝试使用以下代码:

NSDate *date=[NSDate date];
NSDateFormatter *objTimeFotmatter=[[NSDateFormatter alloc]init];
[objTimeFotmatter setDateFormat:@"HH:mm"];
[objTimeFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]
autorelease]];
NSDateFormatter *objDayFotmatter=[[NSDateFormatter alloc]init];
[objDayFotmatter setDateFormat:@"EEEE"];
[objDayFotmatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]
autorelease]];
NSString *objTime=[objTimeFotmatter stringFromDate:date];
NSString *objDay=[objDayFotmatter stringFromDate:date];
[objDayFotmatter release];
[objTimeFormatter release];

NSLog(@"objTime=%@",objTime);
NSLog(@"objDay=%@",objDay);

Try with this code:

NSDate *date=[NSDate date];
NSDateFormatter *objTimeFotmatter=[[NSDateFormatter alloc]init];
[objTimeFotmatter setDateFormat:@"HH:mm"];
[objTimeFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]
autorelease]];
NSDateFormatter *objDayFotmatter=[[NSDateFormatter alloc]init];
[objDayFotmatter setDateFormat:@"EEEE"];
[objDayFotmatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]
autorelease]];
NSString *objTime=[objTimeFotmatter stringFromDate:date];
NSString *objDay=[objDayFotmatter stringFromDate:date];
[objDayFotmatter release];
[objTimeFormatter release];

NSLog(@"objTime=%@",objTime);
NSLog(@"objDay=%@",objDay);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文