获取一天中两次之间的时间 [英] get time between two times of the day

查看:16
本文介绍了获取一天中两次之间的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过使用 NSDate 但没有成功.我想要例如 14:10 和 18:30 之间的差异.

I've already tried with NSDate but with no luck. I want the difference between for example 14:10 and 18:30.

小时和分钟.

我希望你能帮助我不应该那么复杂:)

I Hope you can help me shouldn't be that complicated :)

推荐答案

这个不需要手工计算,看NSCalendar.如果您想获取两个日期之间的小时和分钟,请使用以下内容:

There's no need to calculate this by hand, take a look at NSCalendar. If you want to get the hours and minutes between two dates, use something like this:

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit;
NSDateComponents *components = [gregorianCalendar components:unitFlags
                                                    fromDate:firstDate
                                                      toDate:otherDate
                                                     options:0];
[gregorianCalendar release];

您现在拥有 NSDateComponents 的小时和分钟,并且可以像 [components hours][components minute] 一样以 NSIntegers 访问它们.这也可以在几天之间、闰年和其他有趣的东西之间工作几个小时.

You now have the hours and minutes as NSDateComponents and can access them as NSIntegers like [components hour] and [components minute]. This will also work for hours between days, leap years and other fun stuff.

这篇关于获取一天中两次之间的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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