Date 格式化
module.exports = {
patchZero: function (num, median) {
var tmp = "000000000000000000";
median = median || 2;
num = num + "";
var ns = num.length;
if (ns >= median) {
return num;
}
return tmp.substring(0, median - ns) + num;
},
formatDate(date, fmt) {
const that = this ;
fmt = fmt || "yyyy-MM-dd HH:mm:ss";
return fmt.replace("yyyy", date.getFullYear()).
replace("MM", that.patchZero(date.getMonth()+1)).
replace("dd", that.patchZero(date.getDate())).
replace("HH", that.patchZero(date.getHours())).
replace("mm", that.patchZero(date.getMinutes())).
replace("ss", that.patchZero(date.getSeconds())).
replace("sss",that.patchZero(date.getMilliseconds()));
},
formatTimestamp(ctime,fmt) {
fmt = fmt || "yyyy-MM-dd HH:mm:ss";
var date = new Date();
date.setTime(parseInt(ctime)*1000);
return this.formatDate(date,fmt);
},
};