views:

265

answers:

4

I want to write some functions for that, but before I do: Are there any to convert between time units?

A: 

If you are using .Net platform then you have a solution straight. Use the object called TimeSpan.

TimeSpan holds the differnce between 2 datetime objects. This difference then can be presented as seconds, minutes or hours or days...

Ex:

DateTime d1 = DateTime.Now

DateTime d2 = DateTime.Now.AddHours(100);

TimeSpan _timeDiff = d1 - d2;

Thanks.

this. __curious_geek
+2  A: 

Try the NSDate class

Cade Roux
A: 

Yes there are, the NSDateComponents class be used to convert time intervals to and from NSDate objects, along with an NSCalendar to handle localization issues. It's really only useful when working with dates though. For seconds and minutes, I would just do the conversions myself.

Marc Charbonneau
+1  A: 

To convert hours to seconds you multiply the seconds X 3600. To convert seconds to hours you divide by 3600.

But what if there are remainder minutes and seconds? Well you can do something like this assuming you have an NSTimeInterval of interval number of seconds:

int mins, hours, remainderSeconds, remainderMins;
    mins = interval / 60;
    hours = mins/60;
    remainderMins = mins - (hours * 60);
    remainderSeconds = interval - (mins * 60);

There may be a better way, but this is what I have done in the past and it works.

astar
You can use the modulo operator % to get remainders if you want to code this by hand.
Cade Roux