{{ message }}

Instantly share code, notes, and snippets.

# shunchu/convert-seconds-into-hh-mm-ss-in-ruby.rb

Created Jul 25, 2012
Convert seconds into HH:MM:SS in Ruby
 t = 236 # seconds Time.at(t).utc.strftime("%H:%M:%S") => "00:03:56" # Reference # http://stackoverflow.com/questions/3963930/ruby-rails-how-to-convert-seconds-to-time

### fguillen commented Mar 6, 2014

 I like it! :)

 +1

 +1

### rajdeep26 commented Apr 9, 2014

 Thanks. Helped a lot! :)

### peijiehu commented Jul 23, 2014

 Like! Thanks!

### evanmcc92 commented Aug 19, 2014

 perfect thanks!

### amerem commented Dec 12, 2014

 Awesome! Thank you!

### NayanaBhagat commented Dec 15, 2014

 What if we want to display more than 24hours?, the above solution neglects 24 hours and prints the remaining time since its TIME function Can any one help?

### NayanaBhagat commented Dec 15, 2014

 Got this solution in one of the blogs [total_time / 3600, total_time/ 60 % 60, total_time % 60].map { |t| t.to_s.rjust(2,'0') }.join(':') Here total_time is total number of seconds Works for me 😄

 +1

 +1

 +1

### yogendra689 commented Apr 22, 2015

 +1 thanks

 +1

### springerigor commented Jun 2, 2015

 Using `Time.utc.strftime` works only for values when total number of hours is less then 24: ``````2.2.2 :004 > Time.at(60 * 60).utc.strftime('%H h %M m') => "01 h 00 m" `````` For greater values it returns incorrect results: ``````2.2.2 :006 > Time.at(60 * 60 * 24).utc.strftime('%H h %M m') => "00 h 00 m" `````` I suggest using the simplest method I found for this problem: `````` def formatted_duration total_seconds hours = total_seconds / (60 * 60) minutes = (total_seconds / 60) % 60 seconds = total_seconds % 60 "#{ hours } h #{ minutes } m #{ seconds } s" end `````` You can always adjust returned value to your needs.

 Nice!

### shoaib-iqbal commented Aug 8, 2015

 +1 thanks

### lalitlogical commented Aug 28, 2015

 +1 Nice!

### boomshadow commented Sep 29, 2015

 @springerigor Super +1! Thanks!

### KarthikRamu1 commented Oct 27, 2015

 Thanks!

### TecnoSigma commented Oct 30, 2015

 Thanks!

 +1

 +1

 +1

 ❤️

### petehamilton commented Feb 10, 2016

 ❤️ perfect.

### lonny commented Feb 24, 2016 • edited

 Kudos to NayanaBhagat. I have expanded on her idea just a bit to add optional decimal seconds, which adds to the complexity quite a bit. I have limited the decimals to 8 due to floating point error. (Update: I fixed a bug that wasn't displaying fractional seconds correctly.) ``````def hms(seconds, decimals = 0) int = seconds.floor decs = [decimals, 8].min frac = seconds - int hms = [int / 3600, (int / 60) % 60, int % 60].map { |t| t.to_s.rjust(2,'0') }.join(':') if decs > 0 fp = (frac == 0) ? '.00' : "#{(frac).round(decs)}"[1..-1] hms << fp end hms end ``````

### elquimista commented Mar 3, 2016

 Like it 👍

 +1

 +1

### renandanton commented Jul 3, 2016

 How do i convert a string '1m30s' in seconds time ?

### Madh93 commented Aug 1, 2016

 Like a charm 👍

### whrishi commented Aug 2, 2016

 Gr8. Thanks NayanaBhagat for providing one line solution for greater than 24 hours

### tomascharad commented Aug 23, 2016 • edited

 Amazing. I had a float in hours (i.e.: 6.5), so I just did `Time.at(t.to_hours).utc.strftime("%H:%M:%S")`

 +1

 +1

### lonny commented Jan 1, 2017 • edited

 sweeninalla, just off the top of my head: ``````before = "1:30" hrs, mins = before.split(':') after = "#{hrs} hrs #{mins} mins" ``````

 +1

 +1

### Evanto commented Apr 12, 2017

 If I have 2 integers 07 hours and 15 minutes, how can I convert them to time and get 07:15 time in Ruby?

 +1

 +3

 +1

 +1

### redbar0n commented Sep 8, 2017 • edited

 I have used the solutions of @springerigor and @NayanaBhagat to make it a bit more readable / understandable than a cryptic oneliner. It also removes decimals by using rounding. ```# Will take as input a time in seconds (which is typically a result after subtracting two Time objects), # and return the result in HH:MM:SS, even if it exceeds a 24 hour period. def formatted_duration(total_seconds) hours = total_seconds / (60 * 60) minutes = (total_seconds / 60) % 60 seconds = total_seconds % 60 [hours, minutes, seconds].map do |t| # Right justify and pad with 0 until length is 2. # So if the duration of any of the time components is 0, then it will display as 00 t.round.to_s.rjust(2,'0') end.join(':') end```

 +1

 +1

 +1

### iGEL commented Mar 6, 2018

 If you want to have output like `1h 30m`: ```def seconds_to_str(seconds) ["#{seconds / 3600}h", "#{seconds / 60 % 60}m", "#{seconds % 60}s"] .select { |str| str =~ /[1-9]/ }.join(" ") end seconds_to_str(2) # => "2s" seconds_to_str(69) # => "1m 9s" seconds_to_str(600) # => "10m"* seconds_to_str(43_200) #=> "12h" seconds_to_str(43_205) # => "12h 5s" <- no minutes! seconds_to_str(100_000) # => "27h 46m 40s" <- you could adjust it for days etc.```

 +1

 👍

### dblock commented Jun 26, 2018 • edited

 Beware of `round` being used here. If total seconds is `479.8282647584973` @redbar0n code produces the wrong result, "00:08:60" @lonny code and the original `Time.at` version round down, so "00:07:59", but this is not the best approximation Related and working:

 +1

 +1

 +1

 +1

### alexwebgr commented Apr 3, 2019 • edited

 Added days! extending on @springerigor solution `````` def formatted_duration(total_seconds) days = total_seconds / (60 * 60 * 24) hours = total_seconds / (60 * 60) hours -= 24 if days > 0 minutes = (total_seconds / 60) % 60 seconds = total_seconds % 60 "#{days}d #{hours}h #{minutes}m #{seconds}s" end ``````

 👍

### MelvynSwingler commented Jul 15, 2019

 This is pretty cool!

 👍

 Solid!

### hdchinh commented Mar 31, 2020

 Thanks, really good 😄

 +1

 +1

 +1