Maystro
Maystro

Reputation: 2955

get total hours from nsdate

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,

Upvotes: 0

Views: 1943

Answers (1)

ohr
ohr

Reputation: 1727

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

Upvotes: 3

Related Questions