-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplayground.mjs
146 lines (114 loc) · 3.55 KB
/
playground.mjs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
console.log(
`###### ${
process.env.TZ ||
Intl.DateTimeFormat("en-US", {
timeZoneName: "longGeneric",
})
.format(new Date())
.split(" ")
.slice(1)
.join(" ")
} ######`
);
function handle(fn, ...args) {
const date = new Date(2020, 0, 1);
console.log(`=== date.${fn}(${args.join(", ")}) ===`);
console.log();
print(date);
console.log("->");
date[fn](...args);
print(date);
console.log();
}
console.log();
console.log("************************************");
console.log("********** setUTCFullYear **********");
console.log("************************************");
console.log();
handle("setUTCFullYear", 2020);
handle("setUTCFullYear", 2020, 0);
handle("setUTCFullYear", 2020, 0, 1);
handle("setUTCFullYear", 2020, 48);
handle("setUTCFullYear", 2020, -8);
handle("setUTCFullYear", 2020, 14, 45);
handle("setUTCFullYear", 2020, -8, -60);
console.log();
console.log("************************************");
console.log("************ setUTCMonth ***********");
console.log("************************************");
console.log();
handle("setUTCMonth", 1);
handle("setUTCMonth", 1, 11);
handle("setUTCMonth", 48);
handle("setUTCMonth", -18);
handle("setUTCMonth", 18, 45);
handle("setUTCMonth", -18, -60);
console.log();
console.log("************************************");
console.log("************ setUTCDate ************");
console.log("************************************");
console.log();
handle("setUTCDate", 11);
handle("setUTCDate", 945);
handle("setUTCDate", -60);
console.log();
console.log("------------------------------------");
console.log("--------------- time ---------------");
console.log("------------------------------------");
console.log();
console.log();
console.log("************************************");
console.log("************ setUTCHours ***********");
console.log("************************************");
console.log();
handle("setUTCHours", 12);
handle("setUTCHours", 12, 34, 56, 789);
handle("setUTCHours", 30, 120, 120, 30000);
handle("setUTCHours", -30, -120, -120, -30000);
console.log();
console.log("************************************");
console.log("*********** setUTCMinutes **********");
console.log("************************************");
console.log();
handle("setUTCMinutes", 34);
handle("setUTCMinutes", 34, 56, 789);
handle("setUTCMinutes", 120, 120, 30000);
handle("setUTCMinutes", -120, -120, -30000);
console.log();
console.log("************************************");
console.log("*********** setUTCSeconds **********");
console.log("************************************");
console.log();
handle("setUTCSeconds", 56);
handle("setUTCSeconds", 56, 789);
handle("setUTCSeconds", 120, 30000);
handle("setUTCSeconds", -120, -30000);
console.log();
console.log("************************************");
console.log("******** setUTCMilliseconds ********");
console.log("************************************");
console.log();
handle("setUTCMilliseconds", 789);
handle("setUTCMilliseconds", 30000);
handle("setUTCMilliseconds", -30000);
function print(date) {
console.log(`${format(date)} / ${date.toISOString()} (UTC)`);
}
function format(date) {
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(
date.getDate()
)}T${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(
date.getSeconds()
)}.${pad(date.getMilliseconds(), 3)}${tz(date)}`;
}
function pad(num, length = 2) {
return num.toString().padStart(length, "0");
}
function tz(date) {
return Intl.DateTimeFormat("en-US", {
timeZoneName: "longOffset",
})
.format(date)
.split(" ")[1]
.slice(3);
}