Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show duration (in minutes) to hours

I want to display the duration of a movie. Right now the movie.duration is shown in minutes (integer)

%b Duration: #{ @movie.duration } # 134 mins

Does rails have a time helper to show this is in a more human-readable way? Something like this:

Duration: 2h 23min
like image 457
ltdev Avatar asked Nov 10 '16 22:11

ltdev


Video Answer


1 Answers

distance_of_time_in_words might help you.

You can use it like this:

distance_of_time_in_words(0, 143.minutes)
# => "about 2 hours"

To use an integer / float you'd need to convert to seconds manually:

distance_of_time_in_words(0, 143 * 60)

You could also calculate it like this:

"#{@movie.duration/60}h #{@movie.duration % 60}min"

The division will give you the hours, while the modulo will give you the minutes.


Finally, for the format specified in your question, there's a Gist you can use for the code here.

like image 151
Ziv Galili Avatar answered Oct 21 '22 15:10

Ziv Galili