get total hours from nsdate

最后都变了- 提交于 2019-12-25 14:09:06

问题


I'm just looking for a method that calculates the total hours from nsdate (does the same as the TotalHours method in .net)

example : 11:30:30 -> 11.5 hours

Thanks in advance,


回答1:


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];

Then use this to find the sum.

double totalHours = ((double)hour + (double)minute/60);


来源:https://stackoverflow.com/questions/12750626/get-total-hours-from-nsdate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!