Fork me on GitHub
.net求学者

js时间格式化(yy年MM月dd日 hh:mm)

//时间格式化
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;
}

function formatData(str, regexp) {
    // 转换日期格式
    str = str.replace(/-/g, '/'); // "2010/08/01";
    // 创建日期对象
    var date = new Date(str);
    return date.format(regexp);
}

formatData(data[i].create_time, 'yy年MM月dd日 hh:mm')

 

posted @ 2014-05-14 11:09  hy31337  阅读(1281)  评论(0编辑  收藏  举报
.net求学者