从 nsdate 获取总小时数 [英] get total hours from nsdate
本文介绍了从 nsdate 获取总小时数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是在寻找一种从 nsdate 计算总小时数的方法(与 .net 中的 TotalHours 方法相同)
I'm just looking for a method that calculates the total hours from nsdate (does the same as the TotalHours method in .net)
示例:11:30:30 -> 11.5 小时
example : 11:30:30 -> 11.5 hours
提前致谢,
推荐答案
使用 NSDateComponents
分隔小时和分钟.
Get the hour and minute separated with NSDateComponents
.
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
然后用它来求和.
double totalHours = ((double)hour + (double)minute/60);
这篇关于从 nsdate 获取总小时数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文