-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
268 lines (221 loc) · 6.02 KB
/
app.js
File metadata and controls
268 lines (221 loc) · 6.02 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
const $ = (id) => document.getElementById(id);
const diffStart = $("diff-start");
const diffEnd = $("diff-end");
const diffResult = $("diff-result");
const mathBase = $("math-base");
const mathDays = $("math-days");
const mathResult = $("math-result");
const unixDate = $("unix-date");
const unixInput = $("unix-input");
const unixResult = $("unix-result");
const copyStatus = $("copy-status");
function pad(n) {
return String(n).padStart(2, "0");
}
function toLocalDateInput(date) {
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
}
function toLocalDateTimeInput(date) {
return `${toLocalDateInput(date)}T${pad(date.getHours())}:${pad(date.getMinutes())}`;
}
function setResult(el, text, error = false) {
if (!el) {
return;
}
el.textContent = text;
el.classList.toggle("error", error);
}
function formatDuration(ms) {
const sign = ms < 0 ? -1 : 1;
let rest = Math.abs(ms);
const dayMs = 24 * 60 * 60 * 1000;
const hourMs = 60 * 60 * 1000;
const minMs = 60 * 1000;
const days = Math.floor(rest / dayMs);
rest -= days * dayMs;
const hours = Math.floor(rest / hourMs);
rest -= hours * hourMs;
const mins = Math.floor(rest / minMs);
rest -= mins * minMs;
const secs = Math.floor(rest / 1000);
const parts = [`${days}d`, `${hours}h`, `${mins}m`, `${secs}s`];
return `${sign < 0 ? "-" : ""}${parts.join(" ")}`;
}
function runDiff() {
if (!diffStart || !diffEnd || !diffResult) {
return;
}
const start = new Date(diffStart.value);
const end = new Date(diffEnd.value);
if (Number.isNaN(start.getTime()) || Number.isNaN(end.getTime())) {
setResult(diffResult, "Enter both start and end date/time.", true);
return;
}
const ms = end.getTime() - start.getTime();
const days = ms / (24 * 60 * 60 * 1000);
const hours = ms / (60 * 60 * 1000);
const mins = ms / (60 * 1000);
setResult(
diffResult,
[
`Signed duration: ${formatDuration(ms)}`,
`Days: ${days.toFixed(4)}`,
`Hours: ${hours.toFixed(2)}`,
`Minutes: ${mins.toFixed(1)}`,
].join("\n")
);
}
function formatWeekday(date) {
return new Intl.DateTimeFormat(undefined, { weekday: "long" }).format(date);
}
function runDateMath() {
if (!mathBase || !mathDays || !mathResult) {
return;
}
if (!mathBase.value) {
setResult(mathResult, "Select a base date.", true);
return;
}
const days = Number(mathDays.value);
if (!Number.isFinite(days) || !Number.isInteger(days)) {
setResult(mathResult, "Days must be an integer.", true);
return;
}
const base = new Date(`${mathBase.value}T00:00:00`);
if (Number.isNaN(base.getTime())) {
setResult(mathResult, "Invalid base date.", true);
return;
}
const result = new Date(base);
result.setDate(result.getDate() + days);
setResult(
mathResult,
[
`Result: ${toLocalDateInput(result)} (${formatWeekday(result)})`,
`From: ${toLocalDateInput(base)} (${formatWeekday(base)})`,
`Offset: ${days >= 0 ? "+" : ""}${days} day(s)`,
].join("\n")
);
}
function runUnixFromDate() {
if (!unixDate || !unixResult) {
return;
}
const date = new Date(unixDate.value);
if (Number.isNaN(date.getTime())) {
setResult(unixResult, "Select a date and time first.", true);
return;
}
const ms = date.getTime();
const sec = Math.floor(ms / 1000);
setResult(
unixResult,
[
`Unix seconds: ${sec}`,
`Unix milliseconds: ${ms}`,
`UTC: ${date.toUTCString()}`,
`ISO: ${date.toISOString()}`,
].join("\n")
);
}
function runUnixToDate() {
if (!unixInput || !unixResult) {
return;
}
const raw = unixInput.value.trim();
if (!raw) {
setResult(unixResult, "Enter a Unix value.", true);
return;
}
const value = Number(raw);
if (!Number.isFinite(value)) {
setResult(unixResult, "Unix value must be numeric.", true);
return;
}
const ms = Math.abs(value) < 1e12 ? value * 1000 : value;
const date = new Date(ms);
if (Number.isNaN(date.getTime())) {
setResult(unixResult, "Unix value is out of range.", true);
return;
}
setResult(
unixResult,
[
`Local: ${date.toLocaleString()}`,
`UTC: ${date.toUTCString()}`,
`ISO: ${date.toISOString()}`,
`Detected unit: ${Math.abs(value) < 1e12 ? "seconds" : "milliseconds"}`,
].join("\n")
);
}
async function copyUrl() {
if (!copyStatus) {
return;
}
try {
await navigator.clipboard.writeText(window.location.href);
copyStatus.textContent = "Copied";
} catch (_error) {
copyStatus.textContent = "Copy failed";
}
setTimeout(() => {
copyStatus.textContent = "";
}, 1200);
}
function initDiff(now) {
if (!diffStart || !diffEnd || !diffResult) {
return;
}
const weekAgo = new Date(now);
weekAgo.setDate(weekAgo.getDate() - 7);
diffStart.value = toLocalDateTimeInput(weekAgo);
diffEnd.value = toLocalDateTimeInput(now);
setResult(diffResult, "Pick dates and press Calculate.");
const button = $("diff-run");
if (button) {
button.addEventListener("click", runDiff);
}
}
function initDateMath(now) {
if (!mathBase || !mathDays || !mathResult) {
return;
}
mathBase.value = toLocalDateInput(now);
mathDays.value = "30";
setResult(mathResult, "Pick a date and offset, then Compute.");
const button = $("math-run");
if (button) {
button.addEventListener("click", runDateMath);
}
}
function initUnix(now) {
if (unixDate && unixResult) {
unixDate.value = toLocalDateTimeInput(now);
setResult(unixResult, "Convert in either direction.");
}
const fromButton = $("unix-from-date");
if (fromButton) {
fromButton.addEventListener("click", runUnixFromDate);
}
const toButton = $("unix-to-date");
if (toButton) {
toButton.addEventListener("click", runUnixToDate);
}
}
function initCopy() {
const copyButton = $("copy-link");
if (!copyButton) {
return;
}
copyButton.addEventListener("click", () => {
void copyUrl();
});
}
function init() {
const now = new Date();
initDiff(now);
initDateMath(now);
initUnix(now);
initCopy();
}
init();