4

How to display 2 digits numbers of time left [countdown jquery] ?

My code will show

66days7hours6minutes2seconds

but I want to display 2 digits number e.g.

66days07hours06minutes02seconds

how to do that ?

http://jsfiddle.net/D3E9G/

SW4
  • 67,554
  • 20
  • 126
  • 133
  • possible duplicate of [javascript format number to have 2 digit](http://stackoverflow.com/questions/8043026/javascript-format-number-to-have-2-digit) – OpenStark Aug 05 '14 at 08:31

2 Answers2

5

You can append the leading zeroes, and then use substr to cut the string to the required length:

day = ("00" + day).substr(-2);            
hour = ("00" + hour).substr(-2);            
minute = ("00" + minute).substr(-2);            
second = ("00" + second).substr(-2);

The -2 parameter means that you want to take the 2 characters from the end of the string.

Updated fiddle

Rory McCrossan
  • 319,460
  • 37
  • 290
  • 318
2

Try this

if (day < 10) day = "0" + day;
if (hour < 10) hour = "0" + hour;
if (minute < 10) minute = "0" + minute;
if (second < 10) second = "0" + second;

DEMO

Sridhar R
  • 19,902
  • 6
  • 37
  • 35