NSDateFormatter时间不会根据手机设置更改

编程入门 行业动态 更新时间:2024-10-11 23:15:35
本文介绍了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时间不会根据手机设置更改

本文发布于:2023-11-13 11:05:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584190.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   手机   NSDateFormatter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!