Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert time to float

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

EX:

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

EDIT: My time format is minutes since midnight.

like image 959
Bot Avatar asked Oct 28 '25 17:10

Bot


2 Answers

You can parse the string to get two int numbers and an am/pm part (h, m, and a), and then calculate the fraction as follows:

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;
like image 81
Sergey Kalinichenko Avatar answered Oct 31 '25 11:10

Sergey Kalinichenko


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

Or, for time within a single day, use NSDateFormatter with an "A" date format to produce milliseconds since midnight, then mash on that.

like image 34
Hot Licks Avatar answered Oct 31 '25 11:10

Hot Licks