Skip to content

Instantly share code, notes, and snippets.

@martinbean
Created July 13, 2016 08:03
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save martinbean/2bf88c446be8048814cf02b2641ba276 to your computer and use it in GitHub Desktop.
Save martinbean/2bf88c446be8048814cf02b2641ba276 to your computer and use it in GitHub Desktop.
Convert seconds to HH:MM:SS format in JavaScript.
new Date(seconds * 1000).toISOString().substr(11, 8)
@Sanusihassan
Copy link

great

@shahab570
Copy link

Why multiply seconds into 1000? can anyone explain?

@martinbean
Copy link
Author

@selfnatiq
Copy link

selfnatiq commented Apr 16, 2021

Failed: secToTime(359999) - Expected: '99:59:59', instead got: '03:59:59'

@martinbean
Copy link
Author

Failed: secToTime(359999) - Expected: '99:59:59', instead got: '03:59:59'

Overflow. Should only be used with seconds value of less that 24 hours (86400).

@murphieslaw23
Copy link

awesome one-liner!

@NintenZone
Copy link

Came looking for an answer, and I suppose I found it 😂

@sidelux
Copy link

sidelux commented Jul 8, 2021

Thanks, works well on Chrome but fails in Safari (14.1.1).

@martinbean
Copy link
Author

martinbean commented Jul 8, 2021

Thanks, works well on Chrome but fails in Safari (14.1.1).

@sidelux It works fine in Safari 14.1.1. It’s just basic JavaScript:

Screenshot 2021-07-08 at 14 22 42

@sidelux
Copy link

sidelux commented Jul 8, 2021

Thanks, works well on Chrome but fails in Safari (14.1.1).

@sidelux It works fine in Safari 14.1.1. It’s just basic JavaScript:

Screenshot 2021-07-08 at 14 22 42

Yes, it was another error. Sorry.

@mohsinraza
Copy link

great

@trungnghia112
Copy link

new Date(seconds * 1000).toISOString().substring(11, 19)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment