UIDatePicker:时间模式:初始化时间(一定要简单)

发布于 2024-09-17 18:32:30 字数 552 浏览 2 评论 0原文

所有,

这似乎是一个简单的事情,但我找不到*正确的方法来创建 UIDatePicker,在时间模式下,并将其初始化为特定时间。我不需要日期——只是时间(想想闹钟)。我创建了一个 NSDate 对象:

NSDate * date = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: (NSTimeInterval) delta];
pickerView = [[UIDatePicker alloc] init];  // which should be 'now' right?
pickerView.datePickerMode = UIDatePickerModeTime;  // which creates just the clock
[pickerView setDate:date];

在第一行中,间隔(增量)为零。显示 7:00PM。

这一定很简单,我错过了,但我找不到正确的方法——有人吗?

先感谢您!

:bp:

*是的,我看过了,但显然不在正确的地方:(

All,

This seems like such a simple thing, but I cannot find* the right method to create a UIDatePicker, in time mode, and have it initialized to a specific time. I don't want date -- just time (think alarm clock). I have created a NSDate object:

NSDate * date = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: (NSTimeInterval) delta];
pickerView = [[UIDatePicker alloc] init];  // which should be 'now' right?
pickerView.datePickerMode = UIDatePickerModeTime;  // which creates just the clock
[pickerView setDate:date];

and in the first line, the interval (delta) is zero. It displays 7:00PM.

This has gotta be so simple that I'm missing it, but I can't find the right way -- anyone?

Thank you in advance!

:bp:

*yes, I have looked, but apparently not in the correct places :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伪装你 2024-09-24 18:32:30

考虑时区。仅当您处于 UTC 时,您才会得到 0AM。

Take the time zone into account. You will get 0AM only if you are at UTC.

浅沫记忆 2024-09-24 18:32:30

谢谢你 dkk——我很感谢你的帮助。真正的答案是不使用 DatePicker,而是使用 PickerView(不带日期)。这让事情变得更简单、更可行。
再次感谢。

Thank you dkk -- I appreciate your help. The real answer was to not use a DatePicker, but to use a PickerView (w/o the date). That made things simpler and do-able.
Thanks again.

一江春梦 2024-09-24 18:32:30
NSDate *date = [NSDate date];
date = [[NSCalendar autoupdatingCurrentCalendar] dateFromComponents:[[NSCalendar autoupdatingCurrentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date]];
date = [date dateByAddingTimeInterval:60 * 60 * 21];

pickerView.date = date;

这里涉及日期的步骤是

  • 使用当前日期初始化日期
  • 获取午夜的日期
  • 向日期添加时间间隔(60 秒 * 60 分钟 * 小时)。在本例中,21 = 9pm

这适用​​于 UIDatePickerdatePickerModeUIDatePickerModeTime & UIDatePickerModeCountDownTimer

NSDate *date = [NSDate date];
date = [[NSCalendar autoupdatingCurrentCalendar] dateFromComponents:[[NSCalendar autoupdatingCurrentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date]];
date = [date dateByAddingTimeInterval:60 * 60 * 21];

pickerView.date = date;

The steps here with the date are

  • initialise date with the current date
  • get the date at midnight
  • add a time interval to the date (60 seconds * 60 minutes * hour). In this case 21 = 9pm

This works for UIDatePicker with datePickerMode of UIDatePickerModeTime & UIDatePickerModeCountDownTimer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文