Add 0 in front of every single day or month for date format - Javascript Date Operation

Javascript examples for Date Operation:Month

Description

Add 0 in front of every single day or month for date format

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*from   www  . j  av  a  2s.co m*/
date = "4-1-2013";
date = date.split('-');
day = date[0].length == 1 ? "0" + date[0] : date[0];
month = date[1].length == 1 ? "0" + date[1] : date[1];
date = "" + day + month + date[2];
console.log(date);
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials