现在我把一个日期变量输出后显示Tue Sep 15 09:42:19 UTC+0800 2009 请问我用什么方法格式化成(2009-9-15)大家帮忙看看,谢谢了

解决方案 »

  1.   

    <script>
    var d = new Date("Tue Sep 15 09:42:19 UTC+0800 2009");
    alert(d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate());
    </script>
      

  2.   


    var d = new Date("Tue Sep 15 09:42:19 UTC+0800 2009");
    alert(d.toLocaleDateString())
      

  3.   


      alert(new Date().toLocaleDateString().replace('年','-').replace('月','-').replace('日',''));
      

  4.   

    var t=new Date(),y,n,d,h,m,s,a=new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
    y=t.getFullYear();
    n=t.getMonth()+1;
    if(n<10)n="0"+n;
    d=t.getDate();
    if(d<10)d="0"+d;
    h=t.getHours();
    if(h<10)h="0"+h;
    m=t.getMinutes();
    if(m<10)m="0"+m;
    s=t.getSeconds();
    if(s<10)s="0"+s;
    t=y+"-"+n+"-"+d+" "+h+":"+m+":"+s+" "+a[t.getUTCDay()];
    alert(t);
      

  5.   

    var t=new Date();
    alert(t.getFullYear()+"-"+(t.getMonth()+1)+"-"+t.getDate());
      

  6.   

    给Date对象加一个格式化方法<script>
    Date.prototype.format = function(format)
        {
            var o =
            {
                "M+" : this.getMonth()+1, //month
                "d+" : this.getDate(),    //day
                "h+" : this.getHours(),   //hour
                "m+" : this.getMinutes(), //minute
                "s+" : this.getSeconds(), //second
                "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
                "S" : this.getMilliseconds() //millisecond
            }
            if(/(y+)/.test(format)){
                format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
            }
            for(var k in o){
                if(new RegExp("("+ k +")").test(format)){
                    format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
                }
            }
            return format;
        }
        var d = new Date() ;
        alert(d.format("yyyy")) ;          //年
            alert(d.format("MM")) ;            //月
            alert(d.format("dd")) ;            //日
            alert(d.format("qq")) ;            //季度
            alert(d.format("yyyy-MM-dd")) ;    //年月日
    </script>