Quantcast
Channel: JavaScript seconds to time string with format hh:mm:ss - Stack Overflow
Viewing all articles
Browse latest Browse all 49

Answer by Michael D. Moradzadeh for JavaScript seconds to time string with format hh:mm:ss

$
0
0

I'd upvote artem's answer, but I am a new poster. I did expand on his solution, though not what the OP asked for as follows

    t=(new Date()).toString().split("");    timestring = (t[2]+t[1]+'<b>'+t[4]+'</b> '+t[6][1]+t[7][0]+t[8][0]);

To get

04Oct 16:31:28 PDT

This works for me...

But if you are starting with just a time quantity, I use two functions; one to format and pad, and one to calculate:

function sec2hms(timect){  if(timect=== undefined||timect==0||timect === null){return ''};  //timect is seconds, NOT milliseconds  var se=timect % 60; //the remainder after div by 60  timect = Math.floor(timect/60);  var mi=timect % 60; //the remainder after div by 60  timect = Math.floor(timect/60);  var hr = timect % 24; //the remainder after div by 24  var dy = Math.floor(timect/24);  return padify (se, mi, hr, dy);}function padify (se, mi, hr, dy){  hr = hr<10?"0"+hr:hr;  mi = mi<10?"0"+mi:mi;  se = se<10?"0"+se:se;  dy = dy>0?dy+"d ":"";  return dy+hr+":"+mi+":"+se;}

Viewing all articles
Browse latest Browse all 49

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>