Skip to content Skip to sidebar Skip to footer

Converting Seconds To Time Format

I am trying to find a good solution for converting seconds to time format. I have this function which works fine for my needs so far. function secondstotime(secs) { var t = new

Solution 1:

put the condition below :

if(s.substr(0, 2) == 00)
        return s.substr(3);

working demo http://jsfiddle.net/7Pp5z/2/


Solution 2:

First off you should state your question more clearly.

"Converting Seconds to Time Format" Seconds are one way to represent duration, but some kind of reference is needed to make it relevant.

A.D., UTC, or a duration.

See http://en.wikipedia.org/wiki/ISO_8601 for the win.

Try this in a JavaScript Console:

d = new Date(1920000)
Thu Jan 01 1970 01:32:00 GMT+0100 (Westeuropäische Normalzeit)
d.getUTCMinutes()
32
d.getUTCSeconds()
0

Solution 3:

Here's a function to display a time string in the requested format from a given number of seconds [s]:

function showtime(s){
   var time = new Date(new Date('1970/1/1 00:00').setSeconds(s))
                .toTimeString().split(' ')[0].split(':');
   return (+time[0] ? time[0]+':' : '') +
          (+time[1] || +time[0]  ? time[1] +':' : '') +
           time[2];
}

You can find a demonstration in this jsFiddle.


Post a Comment for "Converting Seconds To Time Format"