ie 与 Chrome 时间格式化问题.

ie 与 Chrome 时间格式化通用:

new Date(res[i].Time.replaceAll("-", "/")).format("yyyy-MM-dd")

replaceAll, format是扩展方法

/*    
所以可以用以下几种方式.:
string.replace(new RegExp(strSearch, 'g'), strReplace);
string:字符串表达式包含要替代的子字符串。
strSearch:被搜索的子字符串。
strReplace:用于替换的子字符串。
*/
String.prototype.replaceAll = function(strSearch, strReplace, ignoreCase) {
    if (!RegExp.prototype.isPrototypeOf(strSearch)) {
        return this.replace(new RegExp(strSearch, (ignoreCase ? "gi" : "g")), strReplace);
    } else {
        return this.replace(strSearch, strReplace);
    }
}

/**
* 将时间转换成固定格式输出
* new Date().toFormat('yyyy-MM-dd HH:mm:ss');
* new Date().toFormat('yyyy/MM/dd hh:mm:ss');
* 只支持关键字(yyyy、MM、dd、HH、hh、mm、ss)HH:表示24小时,hh表示12小时
*/
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;
}

原文地址:https://www.cnblogs.com/chencidi/p/3781877.html