代码:
//时间处理函数
var MyDate = {
Now: {
Now: function () { return new Date(); },
Format: function (str) {
return MyDate.Format(new Date(), str);
}
},
Format: function (date, str) {
if (str == null || str == "") {
str = "yyyy-MM-dd HH:mm:ss";
}
var yyyy = date.getFullYear();
var MM = date.getMonth() + 1;
var dd = date.getDate();
var HH = date.getHours();
var mm = date.getMinutes();
var ss = date.getSeconds();
if (MM >= 1 && MM <= 9) {
MM = "0" + MM;
}
if (dd >= 0 && dd <= 9) {
dd = "0" + dd;
}
if (HH >= 1 && HH <= 9) {
HH = "0" + HH;
}
if (mm >= 0 && mm <= 9) {
mm = "0" + mm;
}
if (ss >= 0 && ss <= 9) {
ss = "0" + ss;
}
return str.replace(/yyyy/g, yyyy).replace(/MM/g, MM).replace(/dd/g, dd).replace(/HH/g, HH).replace(/mm/g, mm).replace(/ss/g, ss);
},
addDays: function (date, day) {
let d = date.valueOf();
d += (day * 24 * 60 * 60 * 1000);
return new Date(d);
},
addHours: function (date, hour) {
let d = date.valueOf();
d += (hour * 60 * 60 * 1000);
return new Date(d);
},
addMinutes: function (date, minute) {
let d = date.valueOf();
d += (minute * 60 * 1000);
return new Date(d);
}
}
调用:
MyDate.Now.Format("yyyy-MM-dd HH:00:00");
MyDate.Now.Format();
MyDate.Now.Format("yyyy-MM-dd HH:mm:ss");
MyDate.Format(new Date());
MyDate.Format(new Date(),"yyyy-MM-dd HH:mm:ss");