<script>
function a(s){return s.replace(/-(\d)(?=\D|$)/g,"-0$1");}
alert(a("2003-1-6"));
alert(a("2003-10-6"));
alert(a("2003-1-16"));
</script>

解决方案 »

  1.   

    <script>
    function MakeArray(n) {
    this.length = n
    return this
    }
    function customDateString(oneDate) { var theMonth = oneDate.getMonth() + 1;
    var theYear = oneDate.getYear(); 
    if (theMonth<10){theMonth="0"+theMonth}
    var theDay = oneDate.getDate()
    if (theDay<10){theDay="0"+theDay}
    if  (theYear < 1900) { theYear = theYear + 1900}; var today = theYear + "-" + theMonth + "-" + theDay; return today;
    }document.write(customDateString(new Date()))</script>
      

  2.   

    呵呵,还是 fason(阿信) 的简练.
      

  3.   

    myblessu(寒飞) :
    谢谢你,但是
    oneDate.getMonth() 会出错。因为
    输入oneDate是2003-7-7系统不认为是日期fason(阿信):您的方法对了。谢谢
      

  4.   

    扩展一下
    function a(s){return s.replace(/([-: ])(\d)(?=\D|$)/g,"$10$2");}
    alert(a("2003-1-6 12:1:1"));
    alert(a("2003-10-6 2:1:5"));
    alert(a("2003-1-16 22:7:5"));
      

  5.   

    你好,在function a(s){return s.replace(/-(\d)(?=\D|$)/g,"-0$1");}中在ie6可以正常运行,但是我们的生产环境都是ie5请问这个函数在ie5中会出错,请问用什么别的方法也实现这个功能呢?
      

  6.   

    <script>
    function a(v) {
      v += "."
      re = /-(\d[^\d])/g;
      while(re.test(v))
        v = v.replace(re,"-0$1");
      return v.substr(0,v.length-1);
    }
    alert(a("2003-1-1"));
    alert(a("2003-11-1"));
    alert(a("2003-1-11"));
    </script>
      

  7.   

    <script>
    function a(v) {
      return v.replace(/\b(\d)\b/g,'0$1')
    }
    alert(a("2003-1-1"));
    alert(a("2003-11-1 12:1:1"));
    alert(a("2003年1月11日"));
    </script>
      

  8.   

    啊信老大,return s.replace(/-(\d)(?=\D|$)/g,"-0$1");
    这句话我看不太懂,什么意思,请指教~~~