UIDatePicker返回值为24小时格式 [英] UIDatePicker return value in 24-hour-format
问题描述
我是iPhone开发的新手,现在正在开发我的第一个应用程序.我在视图上有一个UIDatePicker,如果用户单击保存",则日期选择器的当前时间应保存在数据库中(格式为hh:mm).
I'm new to iPhone development and now working on my first app. I have an UIDatePicker on a view and, if the user clicks on save, the current time of the date picker should be saved in the database (in format hh:mm).
现在,问题在于我的UIDatePicker具有24小时格式来选择所需的小时和分钟(我通过将模拟器中的iPhone设置设置为瑞士"和德语"来配置此设置).例如,如果我在日期选择器中选择17:00,则以下代码将在NSLog中生成5:00:
Now, the problem is that my UIDatePicker has a 24-hour-format to choose the desired hours and minutes (I configured this by setting the iPhone settings in the simulator to "Switzerland" and "German"). If I choose, for example, 17:00 in the date picker, the following code generates 5:00 in NSLog:
NSDateFormatter *datePickerFormat = [[NSDateFormatter alloc] init];
[datePickerFormat setDateFormat:@"hh:mm"];
NSString *datePickerStringToSave = [datePickerFormat stringFromDate:datePicker.date];
NSLog(@"%@", datePickerStringToSave);
日期选择器是否有可能以24小时格式返回所选时间?还是可以配置NSDateFormatter以在24小时内显示返回的时间?我已经尝试在日期选择器的属性检查器中将本地"设置设置为我的国家和语言,但这不起作用...
Is it possible that the date picker returns the selected time in 24-hour format? Or can I configure the NSDateFormatter to show the returned time in 24 hours? I already tried to set the "Local" setting in the attributes inspector of the date picker to my country and language, but that doesn't work...
感谢您的回答!
推荐答案
我认为这里的问题是 @"hh:mm"
中的小写字母h强制了 datePickerFormat
进入12小时模式.尝试将该行更改为:
I think the problem here is that the lowercase h in @"hh:mm"
forces datePickerFormat
into 12 hour mode. Try changing that line to:
[datePickerFormat setDateFormat:@"HH:mm"];
有关正确的日期格式模式的信息,请查看本指南
For information on proper date format patterns, check out this guide.
这篇关于UIDatePicker返回值为24小时格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!