Reputation: 4467
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
Upvotes: 6
Views: 7536
Reputation: 1445
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.
Upvotes: 8
Reputation: 2248
Try Following
def formatted_duration(total_minute)
hours = total_minute / 60
minutes = (total_minute) % 60
"#{ hours }h #{ minutes }min"
end
Upvotes: 7