js 中怎么定义或者重写 SimpleDateFormat 方法

解决方案 »

  1.   


    //时间格式化
    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 now = new Date();
    alert(now.format("yyyy-MM-dd hh:mm:ss"));
      

  2.   

    var now = new Date();
    alert(now.format("yyyy-MM-dd hh:mm:ss"));
    var now2 = new Date();
    alert(now2.format("yyyy-MM-dd"));
    格式在format()方法里通过参数自己定义
      

  3.   

       我不想转化这个Date();
      我想转化其他的,该怎么做
      

  4.   

    其他的也一样,只要数据类型是Date
      

  5.   


    那您给说说吧,假如说我想转一个名叫time 的,这个time 是我从后台取到的
      

  6.   

    var d = new Date(time);