我已经尝试过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.
更多推荐
每天两次之间的时间
发布评论