每天两次之间的时间

编程入门 行业动态 更新时间:2024-10-27 16:30:55
本文介绍了每天两次之间的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经尝试过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的小时和分钟,并可以 NSIntegers访问它们喜欢 [components hour] 和 [components minute] 。这也可以在几天,闰年和其他有趣的东西之间工作几个小时。

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.

更多推荐

每天两次之间的时间

本文发布于:2023-10-27 03:50:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532223.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!