Marciel Fonseca
Marciel Fonseca

Reputation: 381

Display timer correctly (3:45) based on number of seconds

I'm developing a website and using socket.io.I have items auctions and in my database I have set the time(unix timestamp) for each auction to end. When users bid on that item, if is there less than 20 seconds to end then the time on the database must change for it to get back to 20 seconds remaining. On every new bid the seconds left will be 20 seconds. So with that said, that's what we have:

Long story short, what I need is the javascript to calculate the database stored timestamp minus the current unix timestamp (which it already does) and then turn it into 0:00... Let's say database time - current time equals 600 seconds, how should I do to turn it into 5:00 output? Thank You!

SOLUTION: I'm sorry guys, this was really simple to solve and I found a solution after some advice. For those who are facing the same "problem" here is the solution I found and it couldn't be more simple

var time= 300;
var minutes = "0" + Math.floor(time / 60);
var seconds = "0" + (time - minutes * 60);
return minutes.substr(-2) + ":" + seconds.substr(-2);

Credits to the user who gave this answer on this thread: Javascript seconds to minutes and seconds

Upvotes: 1

Views: 380

Answers (2)

GitaarLAB
GitaarLAB

Reputation: 14645

There is a simple fast and short solution to format seconds into M:SS (so without zero-padded minutes as your question asked for) :

function fmtMSS(s){return(s-(s%=60))/60+(9<s?':':':0')+s}

The function accepts either a Number (preferred) or a String (2 conversion 'penalties' which you can halve by prepending + in the function call's argument for s as in: fmtMSS(+strSeconds)), representing positive integer seconds s as argument.

Examples:

fmtMSS(    0 );  //   0:00
fmtMSS(   '8');  //   0:08
fmtMSS(    9 );  //   0:09
fmtMSS(  '10');  //   0:10
fmtMSS(   59 );  //   0:59
fmtMSS( +'60');  //   1:00
fmtMSS(   69 );  //   1:09
fmtMSS( 3599 );  //  59:59
fmtMSS('3600');  //  60:00
fmtMSS('3661');  //  61:01
fmtMSS( 7425 );  // 123:45

Breakdown:

function fmtMSS(s){   // accepts seconds as Number or String. Returns m:ss
  return( s -         // take value s and subtract (will try to convert String to Number)
          ( s %= 60 ) // the new value of s, now holding the remainder of s divided by 60 
                      // (will also try to convert String to Number)
        ) / 60 + (    // and divide the resulting Number by 60 to give minutes
                      // (can never result in a fractional value = no need for rounding)
                      // to which we concatenate a String (converts the Number to String)
                      // who's reference is chosen by the conditional operator:
          9 < s       // if    seconds is larger than 9
          ? ':'       // then  we don't need to prepend a zero
          : ':0'      // else  we do need to prepend a zero
        ) + s ;       // and we add Number s to the string (converting it to String as well)
}

Note: Negative range could be added by prepending (0>s?(s=-s,'-'):'')+ to the return expression (actually, (0>s?(s=-s,'-'):0)+ would work as well).

Upvotes: 1

Sinistralis
Sinistralis

Reputation: 432

You would modulo the seconds by 60. Put the result in minutes and the remainder in seconds. Make sure you account for leading 0's. There might be some kind of string format for this but I don't know off the top of my head. Moment.js would make this very simple as well though, just take the current time, add the seconds, and use one of it's format options with m:ss.

There are a lot of examples on here to reference for moment.js. Here's an example: How to convert seconds to HH:mm:ss in moment.js

Upvotes: 1

Related Questions