Example: 500 seconds should be displayed as 8:00 minutes.
def to_minutes(seconds)
m = (seconds/60).floor
s = (seconds - (m * 60)).round
# add leading zero to one-digit minute
if m < 10
m = "0#{m}"
end
# add leading zero to one-digit second
if s < 10
s = "0#{s}"
end
# return formatted time
return "#{m}:#{s}"
end
This was just what I was looking for. Thanks!
ReplyDeleteMe too. Thanks :-)
ReplyDelete500 Seconds are 8 minutes but never mind. :-)
ReplyDeletePerfect! Thanks for posting this up, it's a handy tidbit to have around and saved me having to dream it up myself.
ReplyDeleteIf I may make a suggestion...
ReplyDeletedef to_minutes(seconds)
m = (seconds/60).floor
s = (seconds - (m * 60)).round
# return formatted time
return "%02d:%02d" % [ m, s ]
end
Perfect! thanks
ReplyDeleteawesome! exactly what I was looking for! Thank you.
ReplyDeleteI made a slight change to it for myself (excuse if poorly coded as newbie)
def to_minutes(seconds)
h = (seconds/60/60).floor
m = (seconds/60).floor
m = m - (h * 60) if h
s = (seconds - (m * 60)).round
# return formatted time
p "%02d:%02d:%02d" % [ h, m, s ]
end
to_minutes(12001)
Correction to my previous comment;
ReplyDeletedef to_minutes(seconds)
h = (seconds/60/60).floor
m = (seconds/60).floor
s = (seconds - (m * 60)).round
m = m - (h * 60) if h
# return formatted time
p "%02d:%02d:%02d" % [ h, m, s ]
end
to_minutes(12001)