Swimming time conversion

What would be the best algorithm / code to convert time to float? I am doing this in Obj-c, but actually all I need is an algorithm and I can make code from there.

Example:

  • 1:30 am to 1.50
  • 2:15 pm to 2:25 pm
  • 5:45 pm to 17.75

EDIT: My time format is minutes since midnight.

+3


source to share


2 answers


You can parse the string to get two digits int

and the am/pm

( h

, m

and a

) part, and then calculate the fraction like this:



int h, m, a;
// set a to 0 for "am", or 1 for "pm"
float time = 60*(a*12+h)+m;
float res = time / 60.0;

      

+3


source


How about NSDate timeIntervalSinceDate? And divide by 3600, of course.



Or, for a time in one day, use an NSDateFormatter with the "A" date format to produce milliseconds since midnight and then slow down.

+2


source







All Articles