JS日期格式化

mac2022-06-30  57

// ****************************// 日期格式化// ****************************Date.prototype.format = function (format) {    var o = {        "M+": this.getMonth() + 1, //month         "d+": this.getDate(),    //day         "h+": this.getHours() % 12 || 12,//hour         "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 formatDate(value) {    if (value == null || value == '') {        return '';    }    var dt;    if (value instanceof Date) {        dt = value;    }    else {        dt = new Date(value);        if (isNaN(dt)) {            value = value.replace(/\/Date\((-?\d+)\)\//, '$1'); //标红的这段是关键代码,将那个长字符串的日期值转换成正常的JS日期格式            dt = new Date();            dt.setTime(value);        }    }    return dt.format("yyyy-MM-dd");  //这里用到一个javascript的Date类型的拓展方法,这个是自己添加的拓展方法,在后面的步骤3定义}function formatDateTime(value) {    if (value == null || value == '') {        return '';    }    var dt;    if (value instanceof Date) {        dt = value;    }    else {        dt = new Date(value);        if (isNaN(dt)) {            value = value.replace(/\/Date\((-?\d+)\)\//, '$1'); //标红的这段是关键代码,将那个长字符串的日期值转换成正常的JS日期格式            dt = new Date();            dt.setTime(value);        }    }    return dt.format("yyyy-MM-dd HH:mm:ss");  //这里用到一个javascript的Date类型的拓展方法,这个是自己添加的拓展方法,在后面的步骤3定义}

转载于:https://www.cnblogs.com/LYunF/archive/2013/05/16/3082518.html

相关资源:JQuery 日期格式化插件,日期格式化,JS日期格式化
最新回复(0)