-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (49 loc) · 1.51 KB
/
index.js
File metadata and controls
57 lines (49 loc) · 1.51 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
function XDate(date) {
var dat;
if (!date) return new Date();
if (date instanceof Date) {
return date;
} else if (/^\d+$/.exec(date)) {
dat = new Date(parseInt(date));
} else {
try {
var d = date.split(/[^0-9]/),
i;
for (i = 3; i < 6; i++) {
d[i] = d[i] || 0;
}
dat = new Date(d[0] || 0, (d[1] - 1) || 0, d[2] || 0, d[3] || 0, d[4] || 0, d[5] || 0);
} catch (e) {
dat = new Date(date);
}
}
return dat;
}
module.exports = function(date, template) {
var dat;
if (!date) return '';
dat = XDate(date);
var hours = dat.getHours(),
minute = dat.getMinutes(),
second = dat.getSeconds(),
day = dat.getDate(),
month = dat.getMonth() + 1;
if (!template) {
template = 'YY年MM月DD日 hh:mm:ss';
}
hours <= 9 && template.match('hh') && (hours = '0' + hours);
minute <= 9 && (minute = '0' + minute);
second <= 9 && (second = '0' + second);
return template
.replace(/["']/g, '')
.replace('XMM', /^\d$/.exec(month) ? '0' + month : month)
.replace('XDD', /^\d$/.exec(day) ? '0' + day : day)
.replace('YY', dat.getFullYear())
.replace('MM', month)
.replace('DD', day)
.replace(/hh|h/, hours)
.replace('mm', minute)
.replace('ss', second)
.replace('WW', ['日', '一', '二', '三', '四', '五', '六'][dat.getDay()]);
}