如何从NSDate对象中检索午夜的小时数? [英] How to retrieve number of hours past midnight from an NSDate object?

查看:93
本文介绍了如何从NSDate对象中检索午夜的小时数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从iPhone项目的UIDatePicker控件中检索午夜的小时数。 datePickerMode 设置为 UIDatePickerModeTime ,所以用户只能设置一个时间,没有日期。当用户完成并关闭UIDatePicker的视图时,可能会检索以下日期(例如):

I need to retrieve the number of hours past midnight from a UIDatePicker control in an iPhone project. datePickerMode is set to UIDatePickerModeTime, so the user only can set a time, no date. When the user is done and dismisses the view the UIDatePicker is on, the following date might be retrieved (as an example):

NSDate *returnTime = timePicker.date;
NSLog(@"returnTime: %@", returnTime); // returns for example @"1970-01-01 10:13:00 PM +0000"

如上所述,我正在寻找午夜的小时数。在上面的例子中,该值应为 22 。我想通过创建一个NSDateFormatter对象来实现这一点,并以24小时制的格式提取小时,因此使用 setDateFormat:@H(资本H而不是'h'):

As said, I'm looking for the number of hours past midnight. In the example above, that value should be 22. I wanted to achieve this by creating an NSDateFormatter object and have it extract the hour of day in 24 hour clock format, thus using setDateFormat:@"H" (capital H rather than 'h'):

NSDateFormatter *formatHour = [[NSDateFormatter alloc] init];
[formatHour setDateFormat:@"H"];
NSInteger intHoursPastMidnight = [[formatHour stringFromDate:returnTime] integerValue];

然而,这并不总是按预期工作。当用户在系统范围偏好(即用户使用AM / PM)中禁用24小时制时, intHoursPastMidnight 将包含 10 而不是 22 10 确实是在 UIDatePicker 中可见的值,但我预计 NSDateFormatter 将其转换为 22 ,因为 @H

This however does not always work as expected. When the user has disabled the 24 hour clock in the system wide Preferences (i.e. the user uses AM/PM), intHoursPastMidnight will contain 10 rather than 22. 10 is indeed the value that's visible in the UIDatePicker, but I had expected NSDateFormatter to convert this to 22 because of @"H".

这里有什么问题?我的转换假设是不正确的吗?这是UIDatePicker的错误吗?如何解决问题,所以我可以基本上访问UIDatePicker中设置的午夜之后的小时数,独立于用户的12或24小时时钟偏好?这是否是要走的路?

What's wrong here? Is my convert assumption incorrect? Is this a bug with UIDatePicker? How can I solve the problem, so I can essentially access the number of hours past midnight set in the UIDatePicker, independent from the user's 12 or 24 hour clock preference? Is this the way to go anyway?

最后的目标,要清楚,是检索午夜的分钟数(所以小时* 60 +分钟,值总是在0和1440之间)

The end goal, to make it clear, is to retrieve the number of minutes past midnight (so hours*60+minutes, value always betweet 0 and 1440).

推荐答案

可以使用 NSCalendar

首先,获取日期:

NSDate *date = [timePicker date];

接下来,将其转换为日期组件

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSIntegerMax fromDate:date];

现在我们将重置日期组件的小时和分钟,以便现在指向午夜:

Now we'll reset the hours and minutes of the date components so that it's now pointing at midnight:

[components setHour:0];
[components setMinute:0];
[components setSecond:0];

接下来,我们将其重新设置为日期:

Next, we'll turn it back in to a date:

NSDate *midnight = [[NSCalendar currentCalendar] dateFromComponents:components];

最后,我们会要求午夜和日期之间的时间:

Finally, we'll ask for the hours between midnight and the date:

NSDateComponents *diff = [[NSCalendar currentCalendar] components:NSHourCalendarUnit fromDate:midnight toDate:date options:0];

NSInteger numberOfHoursPastMidnight = [diff hour];

这篇关于如何从NSDate对象中检索午夜的小时数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆