nulltek
nulltek

Reputation: 3337

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?

Upvotes: 4

Views: 6531

Answers (2)

davidrac
davidrac

Reputation: 10738

You can try to use dotiw or ruby-duration gem. It may simplify things for you.

Upvotes: 0

William
William

Reputation: 3529

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.

Upvotes: 5

Related Questions