-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.js
47 lines (41 loc) · 1.06 KB
/
util.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
function isDate(date) {
return Object.prototype.toString.call(date) == "[object Date]";
}
function parseDate(date) {
if (!isDate(date)) {
date = new Date(date);
}
const year = date.getFullYear();
const month = ("" + (date.getMonth() + 1)).padStart(2, "0");
const day = ("" + date.getDate()).padStart(2, "0");
const hours = ("" + date.getHours()).padStart(2, "0");
const minutes = ("" + date.getMinutes()).padStart(2, "0");
const seconds = ("" + date.getSeconds()).padStart(2, "0");
return {
year,
month,
day,
hours,
minutes,
seconds,
};
}
function formatDate(date) {
const { year, month, day } = parseDate(date);
return `${year}-${month}-${day}`;
}
function formatDate2(date) {
const { year, month, day } = parseDate(date);
return `${year}${month}${day}`;
}
function formatDate3(date) {
const { year, month, day, hours, minutes, seconds } = parseDate(date);
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
module.exports = {
isDate,
parseDate,
formatDate,
formatDate2,
formatDate3,
};