Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting float time in rails

I have a datetime called dispatched_time which I am subtracting from Time.now which results in a float number ie: 302.332 which is a method called elapsed_time. I need to figure out a way to take the value of that number and format it into a time in my view. ie: 13:20.

I've tried to use elapsed_time.strftime("%H%M") but strftime won't process a float.

Can someone help me out with converting the float in seconds into a HH:MM format?

like image 881
nulltek Avatar asked Sep 12 '25 18:09

nulltek


1 Answers

You could try Time.at(elapsed_time).utc.strftime("%H:%M"). This should give you the hours/minutes between the two times, created as if it were a time itself. Note that this may not work as well if there are more than 24 hours between the two times, in which case you would need to add a days parameter ("%d") to the string passed to strftime.

like image 119
William Avatar answered Sep 15 '25 00:09

William