Nsdateformatter日返回我在选择手机的语言。它可以返回我只有英语所有的时间?

问题描述 投票:19回答:3

我使用dateformatter得到天的时间在我的应用程序。但是,当我换手机的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);

当我选择手机语言为Gujrati(印度),我用用,这是如下的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=ગુરુવાર
iphone ios4 ios5 nsdate nsdateformatter
3个回答
46
投票

添加以下行到dateformatter。

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];

同样,对于所有dateformatters。


3
投票

迅速

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)

1
投票

尝试使用此代码:

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);
© www.soinside.com 2019 - 2024. All rights reserved.