/**
* 为Date[日期]增加日期格式转换功能
* @param {string} mask 日期格式
* @return {string} 返回mask格式的日期
*/
Date.prototype.format = function(mask) {
var d = this;
//补零操作
var zeroize = function(value, length) {
length || (length = 2);
value = String(value);
for (var i = 0, zeros = ""; i < length - value.length; i++) {
zeros += "0";
}
return zeros + value;
};
return mask.replace(/"[^"]*"|'[^']*'|\b(?:d{1,4}|m{1,4}|yy(?:yy)?|([hHMstT])\1?|[lLZ])\b/g, function($0) {
switch ($0) {
case "dd":
return zeroize(d.getDate());
case "MM":
return zeroize(d.getMonth() + 1);
case "yy":
return String(d.getFullYear()).substr(2);
case "yyyy":
return d.getFullYear();
case "hh":
return zeroize(d.getHours() % 12 || 12);
case "HH":
return zeroize(d.getHours());
case "m":
return d.getMinutes();
case "mm":
return zeroize(d.getMinutes());
case "ss":
return zeroize(d.getSeconds());
case "tt":
return d.getHours() < 12 ? "am" : "pm";
case "TT":
return d.getHours() < 12 ? "AM" : "PM";
case "Z":
return d.toUTCString().match(/[A-Z]+$/);
default:
return $0.substr(1, $0.length - 2);
}
});
};
console.log(new Date().format("yyyy-MM-dd-hh-mm-ss TT Z"))
console