找的网上的js日期格式化问题出错了显示 一堆 NaN的东西

//格式化时间,用法new Date(value).format("yyyy-MM-dd hh:mm:ss")
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;
}

发现有的电脑的右下角日期为 2000/1/1  而有的 为 - 例如 2000-1-1

这个方法会在-的情况下出错,所以使用的时候需要将传入的日期串替换一下 strDate 变为 strDate.replace(new RegExp("-","g"),"/");

这时候就正常了。

原文地址:https://www.cnblogs.com/hbhzz/p/3768539.html