1

I have this script and works great, but I want to change it for two digits, I mean, If left 2 days then show it as 02 days. This is the script:

    <script>
function renderMessage(dateStr, msg1, msg2, countFrom) {
  var date = new Date(dateStr);
  var now = new Date();
  var diff = date.getTime() - now.getTime();
  var days = Math.floor(diff / (1000 * 60 * 60 * 24)) + 1;
  if(days < 1) {
    document.write(msg1);
  } else {
    if(countFrom)
      days = countFrom - days;
    document.write(msg2.replace(/%days%/g, number_format(days)));
  }
}
function number_format(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num)) {
    num = "0";
  }
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  num = Math.floor(num/100).toString();
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
  num = num.substring(0,num.length-(4*i+3))+','+
  num.substring(num.length-(4*i+3));
  return (((sign)?'':'-') + num);
}
</script>


<script>
  renderMessage("November 29, 2012", "You missed it!", "Hurry, there's only %days% days to go!");
</script>

I really appreciate your help.

  • 3
    take a look at this previous question: http://stackoverflow.com/questions/1267283/how-can-i-create-a-zerofilled-value-using-javascript – inki Nov 21 '12 at 18:11

1 Answers1

1

Add in a conditional for any number less than 10:

  if (num < 10)
    num = "0" + Math.floor(num/100).toString();
  else
    num = Math.floor(num/100).toString();
JohnB
  • 1,231
  • 1
  • 18
  • 33