-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.js
72 lines (69 loc) · 1.91 KB
/
format.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
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* YY: 18, The year
* YYYY: 2018
* M: 1-12, The month
* MM: 01-12
* D: 1-31, The day of the month
* DD: 01-31
* d: 0-6, The day of the week, with Sunday as 0
* H: 0-23, The hour
* HH: 00-23
* h: 1-12, The hour, 12-hour clock
* hh: 01-12
* m: 0-59, The minute
* mm: 00-59
* s: 0-59, The second
* ss: 00-59
* SSS: 000-999, The millisecond
* A: AM/PM
* a: am/pm
*/
// format(new Date(), 'YYYY-MM-DD HH:mm:ss'); // 2020-02-13 22:15:20
function format(date, formatStr) {
const formattingTokens = /(YYYY|YY|MM?|DD?|d|HH?|hh?|mm?|ss?|SSS|A|a)/g;
const padStartZero = (str, len = 2) => String(str).padStart(len, '0');
const result = formatStr.replace(formattingTokens, match => {
switch (match) {
case 'YYYY':
return date.getFullYear();
case 'YY':
return String(date.getFullYear()).slice(-2);
case 'MM':
return padStartZero(date.getMonth() + 1);
case 'M':
return `${date.getMonth() + 1}`;
case 'DD':
return padStartZero(date.getDate());
case 'D':
return `${date.getDate()}`;
case 'd':
return `${date.getDay()}`;
case 'HH':
return padStartZero(date.getHours());
case 'H':
return `${date.getHours()}`;
case 'hh':
return padStartZero(date.getHours() % 12);
case 'h':
return `${date.getHours() % 12}`;
case 'mm':
return padStartZero(date.getMinutes());
case 'm':
return `${date.getMinutes()}`;
case 'ss':
return padStartZero(date.getSeconds());
case 's':
return `${date.getSeconds()}`;
case 'SSS':
return padStartZero(date.getMilliseconds(), 3);
case 'A':
return date.getHours() < 12 ? 'AM' : 'PM';
case 'a':
return date.getHours() < 12 ? 'am' : 'pm';
default:
return '';
}
});
return result;
}
module.exports = format;