简体   繁体   English

iPhone-在UIDatePicker上调整日期

[英]iPhone - adjusting the date on a UIDatePicker

I have the hour and the minute as NSIntegers (I don't care about the seconds... it may be 00) and I have a UIDatePicker adjusted to display just time (UIDatePickerModeTime). 我将小时和分钟作为NSIntegers(我不在乎秒...可能是00),并且将UIDatePicker调整为仅显示时间(UIDatePickerModeTime)。

Simple question: how do I set an hour on the UIDatePicker? 一个简单的问题:如何在UIDatePicker上设置一个小时? Hours are in the 24h format (as it comes from the picker naturally). 小时采用24小时制(因为它自然来自选择器)。

Suppose this: 假设这:

NSInteger hour = 17;
NSInteger minute = 12;
[myTimePicker setDate: ????];

I know I have to convert that hour and minute to NSDate, but how do I do that without including a day? 我知道我必须将小时和分钟转换为NSDate,但是如何在不包括一天的情况下进行转换呢? I just need time. 我只需要时间

I have read this example , but it also considers the day. 我已经阅读了这个示例 ,但是它也考虑了这一天。

How do I construct a NSDate of just time, so I can adjust my UIDatePicker? 我该如何构造一个NSDate时间,以便可以调整UIDatePicker?

thanks 谢谢

You should be able to do this, 您应该能够做到这一点,

NSCalendar * gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents * dateComponents = [[[NSDateComponents alloc] init] autorelease];
[dateComponents setHour:17];
[dateComponents setMinute:12];

myTimePicker.date = [gregorian dateFromComponents:dateComponents];

You can later get the hour and minute like this, 以后您可以这样获取小时和分钟,

NSCalendar * gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents * dateComponents = [gregorian components:(NSHourCalendarUnit|NSMinuteCalendarUnit) fromDate:myTimePicker.date];

int hour = [dateComponents hour];
int minute = [dateComponents minute];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM