NSDateFormatter时间不会根据手机设置更改 [英] NSDateFormatter time not changing according to phones settings

查看:64
本文介绍了NSDateFormatter时间不会根据手机设置更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是杀了我,我有以下日期格式化程序:

this one is killing me, I have the following date formatter:

+ (NSDateFormatter *)dateFormatter {
    static NSDateFormatter *_dateFormatter = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSString *format = [NSDateFormatter dateFormatFromTemplate:@"hh:mm a" options:0 locale:[NSLocale currentLocale]];
        _dateFormatter = [[NSDateFormatter alloc] init];
        [_dateFormatter setDateFormat:format];
    });
    return _dateFormatter;
}   

我希望最终的小时格式根据用户的时钟设置而定,因此,如果用户使用"24小时制",则结果应为:

I want the final hour format to be according to the user clock settings, so if the user uses "24-Hour Time", the result should be for example:

19:41

否则结果应该是

7:41 PM

现在好了,杀了我"部分,如果我将手机的区域格式"设置为美国",这似乎非常有用.
如果我打开/关闭"24小时制",则日期实际上会从19:41更改为7:41 PM

ok now for the "killing me" part, this seems to work great if I set the phone's 'Region Format' to 'United States'.
If I toggle the "24-Hour Time" on/off the date indeed changes from 19:41 to 7:41 PM

但是,如果我将地区格式"设置为与美国"不同的名称,例如西班牙",那么无论我是否打开/关闭"24小时制",我总是会得到7:41 pm.

BUT if I set the 'Region Format' to something different than 'United States', for example 'Spain' then I always get 7:41 pm no matter if I change the "24-Hour Time" on/off.

更奇怪的是,如果我使用以下模板:

Even more weird is the fact that if I use the following template:

@"HH:mm a" (changed hh to uppercase HH)

现在,西班牙"功能很好,并且可以根据"24小时制"设置进行更改,但是现在,美国"始终为24小时制,无论我是否在设置中将其关闭!

Now 'Spain' works great and changes according to the "24-Hour Time" settings, BUT now the 'United State' is always in 24 hour format, no matter if I switch it off in settings!

这是怎么回事?!

请考虑使用dateFormatFromTemplate而不是常规格式对我来说很重要.

Please consider that its important to me to use dateFormatFromTemplate and not regular format.

推荐答案

尝试此代码

NSDate *time;
if ([objShare checkTimeFormat]){
    [timeFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];;
    [timeFormat setDateFormat:@"hh:mm a"];
    NSLog(@"%@",lblVtime.text);
    time = [timeFormat dateFromString:lblVtime.text];
}
else{
     NSLog(@"%@",lblVtime.text);
    [timeFormat setDateFormat:@"hh:mm a"];
    time = [timeFormat dateFromString:lblVtime.text];
}
[timeFormat setDateFormat:@"HH:mm"];
NSLog(@"%@",[timeFormat stringFromDate:time]);

让我知道您是否有任何疑问

Let me know if you have any query

checkTimeFormat方法:

-(BOOL)checkTimeFormat{
     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     [formatter setLocale:[NSLocale currentLocale]];
     [formatter setDateStyle:NSDateFormatterNoStyle];
     [formatter setTimeStyle:NSDateFormatterShortStyle];
     NSString *dateString = [formatter stringFromDate:[NSDate date]];
     NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
     NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
     BOOL is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);
     return is24h;
}

这篇关于NSDateFormatter时间不会根据手机设置更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆