-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
26 lines (26 loc) · 857 Bytes
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
exports.DateFormart = function (fmt, date = new Date()) {
var o = {
"y+": date.getFullYear(),
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
"S+": date.getMilliseconds() //毫秒
};
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
if (k == "y+") {
fmt = fmt.replace(RegExp.$1, ("" + o[k]).substr(4 - RegExp.$1.length));
} else if (k == "S+") {
var lens = RegExp.$1.length;
lens = lens == 1 ? 3 : lens;
fmt = fmt.replace(RegExp.$1, ("00" + o[k]).substr(("" + o[k]).length - 1, lens));
} else {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
}
return fmt;
}