使用 24 - 12 小时格式的 xcode 在“TimePicker”中设置 NSDate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11257127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:44:08  来源:igfitidea点击:

Setting NSDate in a "TimePicker" with 24 - 12 hours format xcode

iphoneiosxcodensdatensdateformatter

提问by Jimmy G

I'm working with some NSDate objects, and well, I use my iPhone with 24 hrs format, so when I was testing my app, everything went fine, but, one of my friends tried the app on his iPhone, but he uses 12hrs format, after some research I found out that the problem was the date, and I have no idea why I use this to set a date picker and a time picker, so I read some values from a database (yes, I've checked the values and they are correct, it is not a value-issue), so all this data is formatted and put together as a NSDate, then, I set the date picker and time picker with this date I just created

我正在处理一些 NSDate 对象,好吧,我使用 24 小时格式的 iPhone,所以当我测试我的应用程序时,一切正常,但是,我的一位朋友在他的 iPhone 上尝试了该应用程序,但他使用了 12 小时格式,经过一番研究,我发现问题出在日期上,我不知道为什么我用它来设置日期选择器和时间选择器,所以我从数据库中读取了一些值(是的,我已经检查了值并且它们是正确的,这不是一个值问题),所以所有这些数据都被格式化并作为一个 NSDate 放在一起,然后,我用我刚刚创建的这个日期设置日期选择器和时间选择器

NSDateFormatter *currentDate = [[[NSDateFormatter alloc] init] autorelease];
[currentDate setDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate * startDate =[currentDate dateFromString:[NSString stringWithFormat:@"%d-%d-%d %d:%d",yearVal,monthVal,dayVal,hourVal,minVal]];
[datePicker setDate:startDate];
[timePicker setDate:startDate];

So, yearVal, monthVal, dayVal, hourVal, minVal are all NSIntegers... the thing is, that, it perfectly works if your iPhone/iPod is using 24 hours in your clock settings, but, if you are using 12 hours format, it crashes and shows this error message

所以,yearVal、monthVal、dayVal、hourVal、minVal 都是 NSIntegers ......事实是,如果你的 iPhone/iPod 在你的时钟设置中使用 24 小时,它完美地工作,但是,如果你使用 12 小时格式,它崩溃并显示此错误消息

> *** Assertion failure in -[UIDatePickerView _updateBitsForDate:forced:andReload:animateIfNeeded:], /SourceCache/UIKit/UIKit-1914.85/UIDatePicker.m:1200 2012-06-29
> 01:19:15.753 MyApp[1215:707] *** Terminating app due to uncaught
> exception 'NSInternalInconsistencyException', reason: 'Invalid
> parameter not satisfying: date'
> *** First throw call stack: (0x3710188f 0x34e06259 0x37101789 0x379403a3 0x31042807 0x30f616c5 0x30f61563 0x31042385 0xf48ef 0xf7bd3
> 0x30e0ec8b 0x30e1b1e9 0x30e1b059 0x30e1af3f 0x30e1a7c1 0x30e1a503
> 0x30e0eaff 0x30e0e7d5 0xdd945 0x30e8793d 0x30f01627 0x37972933
> 0x370d5a33 0x370d5699 0x370d426f 0x370574a5 0x3705736d 0x33816439
> 0x30e03cd5 0xd4cbb 0xd4c60) terminate called throwing an
> exception(gdb)

So please, if you have any idea of how to fix this, I would appreciate your help

所以,如果您对如何解决此问题有任何想法,我将不胜感激

oh, and I've read that HH returns hours in 24 hours format, well, all my app uses HH, so i don't understand why I keep watching some "a.m." or "p.m." in some of the NSLogs i print to debug

哦,我已经读过 HH 以 24 小时格式返回小时,好吧,我所有的应用程序都使用 HH,所以我不明白为什么我一直在我打印的某些 NSLog 中观看一些“am”或“pm”调试

Thanks in advance

提前致谢

oh sorry, I almost forget if i comment the lines

哦对不起,我差点忘了我是否评论这些台词

[datePicker setDate:startDate];
[timePicker setDate:startDate];

the app doesn't crash, but of course, it doesn't show the time and date I require

该应用程序不会崩溃,但当然,它不会显示我需要的时间和日期

NOTE: I need to use 24 hours format, if I decide to select format with am pm I will have to change all my app, and well, I don't have enough time for that

注意:我需要使用 24 小时格式,如果我决定使用 am pm 选择格式,我将不得不更改我所有的应用程序,而且我没有足够的时间

回答by Kevin Horgan

Take a look at this like: Find if user prefers 12 / 24 Hour Clock?… think the second answer might be the best for you. Then depending on if the user's device uses a 12 hour clock or 24 hour clock you can set your [currentDate setDateFormat:....] accordingly.

看看这样的:查找用户是否喜欢 12 / 24 小时制?...认为第二个答案可能最适合您。然后,根据用户的设备是使用 12 小时制还是 24 小时制,您可以相应地设置 [currentDate setDateFormat:....]。

If you want a 12 hour clock then set currentDate as follows:

如果您想要 12 小时制,请按如下方式设置 currentDate:

[currentDate setDateFormat:@"yyyy-MM-dd h:mm:ss a"];

If you want a 24 hour clock then set currentDate as follows:

如果您想要 24 小时制,请按如下方式设置 currentDate:

[currentDate setDateFormat:@"yyyy-MM-dd HH:mm"];