時間格式化

代碼
var utils = {
    //字符串添加0
    digit: function(e, t) {
        var i = "";
        e = String(e), t = t || 2;
        for (var o = e.length; o < t; o++) i += "0";
        return e < Math.pow(10, t) ? i + (0 | e) : e
    },
    //格式化時間
    toDateString: function(e, t) {
        var i = this,
            o = new Date(e || new Date),
            a = [i.digit(o.getFullYear(), 4), 
            i.digit(o.getMonth() + 1), 
            i.digit(o.getDate())],
            r = [i.digit(o.getHours()), i.digit(o.getMinutes()), i.digit(o.getSeconds())];
            return t = t || "yyyy-MM-dd HH:mm:ss", t.replace(/yyyy/g, a[0]).replace(/MM/g, a[1]).replace(/dd/g, a[2]).replace(/HH/g, r[0]).replace(/mm/g, r[1]).replace(/ss/g, r[2])
        }
    };
    //調用
    utils.toDateString("yyyy-MM-dd hh:mm");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章