-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimestamp.html
More file actions
494 lines (425 loc) · 17.1 KB
/
timestamp.html
File metadata and controls
494 lines (425 loc) · 17.1 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Timestamp Converter</title>
<link rel="stylesheet" href="common.css">
<style>
body {
display: flex;
flex-direction: column;
}
.converter-section {
max-width: 800px;
margin: 0 auto;
width: 100%;
}
.input-group {
background: var(--card-bg);
border: 1px solid var(--card-border);
border-radius: var(--radius-lg);
box-shadow: var(--shadow-sm);
padding: 20px;
margin-bottom: 16px;
}
.input-group-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 16px;
}
.input-group-title {
font-weight: 600;
font-size: 0.9rem;
text-transform: uppercase;
letter-spacing: 0.5px;
color: var(--text-secondary);
}
.input-row {
display: flex;
gap: 12px;
margin-bottom: 12px;
}
.input-row:last-child {
margin-bottom: 0;
}
.input-field {
flex: 1;
padding: 10px 12px;
border: 1px solid var(--input-border);
border-radius: var(--radius-md);
font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
font-size: 0.875rem;
background: var(--input-bg);
color: var(--text-primary);
outline: none;
box-shadow: var(--shadow-xs);
transition: border-color 0.15s, box-shadow 0.15s;
}
.input-field:hover {
border-color: var(--text-muted);
}
.input-field:focus {
border-color: var(--input-border-focus);
box-shadow: var(--shadow-xs), 0 0 0 3px var(--ring-color);
}
.input-field::placeholder {
color: var(--text-secondary);
font-family: inherit;
}
select.input-field {
cursor: pointer;
}
.output-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 16px;
}
.output-item {
background: var(--card-bg);
border: 1px solid var(--card-border);
border-radius: var(--radius-md);
padding: 12px 16px;
cursor: pointer;
transition: border-color 0.15s, box-shadow 0.15s;
}
.output-item:hover {
border-color: var(--accent-blue);
}
.output-item.copied {
border-color: var(--accent-mint);
box-shadow: 0 0 0 3px rgba(107, 203, 119, 0.2);
}
.output-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 4px;
}
.output-label {
font-size: 0.75rem;
color: var(--text-secondary);
text-transform: uppercase;
letter-spacing: 0.5px;
}
.copy-hint {
font-size: 0.7rem;
color: var(--text-muted);
opacity: 0;
transition: opacity 0.15s;
}
.output-item:hover .copy-hint {
opacity: 1;
}
.output-value {
font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
font-size: 0.875rem;
color: var(--text-primary);
word-break: break-all;
}
.now-btn {
padding: 14px 20px;
white-space: nowrap;
}
.datetime-inputs {
display: flex;
gap: 12px;
flex-wrap: wrap;
}
.datetime-inputs .input-field {
min-width: 150px;
}
@media (max-width: 768px) {
.input-row {
flex-direction: column;
}
.datetime-inputs {
flex-direction: column;
}
}
</style>
</head>
<body>
<a href="index.html" class="back-link">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M19 12H5M12 19l-7-7 7-7"/>
</svg>
All Tools
</a>
<header>
<h1>Timestamp Converter</h1>
<p class="subtitle">Convert between Unix timestamps and human-readable dates</p>
</header>
<div class="converter-section">
<div class="input-group">
<div class="input-group-header">
<span class="input-group-title">Unix Timestamp</span>
<button id="nowBtn" class="now-btn">Now</button>
</div>
<div class="input-row">
<input type="text" id="timestampInput" class="input-field" placeholder="Enter Unix timestamp (seconds or milliseconds)...">
</div>
</div>
<div class="input-group">
<div class="input-group-header">
<span class="input-group-title">Date & Time</span>
</div>
<div class="datetime-inputs">
<input type="date" id="dateInput" class="input-field">
<input type="time" id="timeInput" class="input-field" step="1">
<select id="timezoneSelect" class="input-field">
<!-- Populated by JavaScript -->
</select>
</div>
</div>
<div class="input-group">
<div class="input-group-header">
<span class="input-group-title">Results</span>
</div>
<div class="output-grid">
<div class="output-item" data-copy="outputSeconds">
<div class="output-header">
<span class="output-label">Seconds</span>
<span class="copy-hint">Click to copy</span>
</div>
<div id="outputSeconds" class="output-value">—</div>
</div>
<div class="output-item" data-copy="outputMillis">
<div class="output-header">
<span class="output-label">Milliseconds</span>
<span class="copy-hint">Click to copy</span>
</div>
<div id="outputMillis" class="output-value">—</div>
</div>
<div class="output-item" data-copy="outputISO">
<div class="output-header">
<span class="output-label">ISO 8601</span>
<span class="copy-hint">Click to copy</span>
</div>
<div id="outputISO" class="output-value">—</div>
</div>
<div class="output-item" data-copy="outputUTC">
<div class="output-header">
<span class="output-label">UTC</span>
<span class="copy-hint">Click to copy</span>
</div>
<div id="outputUTC" class="output-value">—</div>
</div>
<div class="output-item" data-copy="outputLocal">
<div class="output-header">
<span class="output-label">Local Time</span>
<span class="copy-hint">Click to copy</span>
</div>
<div id="outputLocal" class="output-value">—</div>
</div>
<div class="output-item" data-copy="outputRelative">
<div class="output-header">
<span class="output-label">Relative</span>
<span class="copy-hint">Click to copy</span>
</div>
<div id="outputRelative" class="output-value">—</div>
</div>
</div>
</div>
</div>
<script>
const timestampInput = document.getElementById('timestampInput');
const dateInput = document.getElementById('dateInput');
const timeInput = document.getElementById('timeInput');
const timezoneSelect = document.getElementById('timezoneSelect');
const nowBtn = document.getElementById('nowBtn');
const outputSeconds = document.getElementById('outputSeconds');
const outputMillis = document.getElementById('outputMillis');
const outputISO = document.getElementById('outputISO');
const outputUTC = document.getElementById('outputUTC');
const outputLocal = document.getElementById('outputLocal');
const outputRelative = document.getElementById('outputRelative');
let updating = false;
// Populate timezone select
const timezones = Intl.supportedValuesOf('timeZone');
const localTz = Intl.DateTimeFormat().resolvedOptions().timeZone;
// Add common timezones at the top
const commonTz = ['UTC', localTz, 'America/New_York', 'America/Los_Angeles', 'Europe/London', 'Europe/Paris', 'Asia/Tokyo', 'Asia/Shanghai'];
const uniqueCommon = [...new Set(commonTz)];
uniqueCommon.forEach(tz => {
const option = document.createElement('option');
option.value = tz;
option.textContent = tz + (tz === localTz ? ' (local)' : '');
option.selected = tz === localTz;
timezoneSelect.appendChild(option);
});
const separator = document.createElement('option');
separator.disabled = true;
separator.textContent = '──────────';
timezoneSelect.appendChild(separator);
timezones.forEach(tz => {
if (!uniqueCommon.includes(tz)) {
const option = document.createElement('option');
option.value = tz;
option.textContent = tz;
timezoneSelect.appendChild(option);
}
});
function getRelativeTime(date) {
const now = new Date();
const diffMs = date - now;
const diffSec = Math.round(diffMs / 1000);
const diffMin = Math.round(diffSec / 60);
const diffHour = Math.round(diffMin / 60);
const diffDay = Math.round(diffHour / 24);
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
if (Math.abs(diffSec) < 60) return rtf.format(diffSec, 'second');
if (Math.abs(diffMin) < 60) return rtf.format(diffMin, 'minute');
if (Math.abs(diffHour) < 24) return rtf.format(diffHour, 'hour');
if (Math.abs(diffDay) < 30) return rtf.format(diffDay, 'day');
const diffMonth = Math.round(diffDay / 30);
if (Math.abs(diffMonth) < 12) return rtf.format(diffMonth, 'month');
const diffYear = Math.round(diffDay / 365);
return rtf.format(diffYear, 'year');
}
function updateOutputs(date) {
if (!date || isNaN(date.getTime())) {
outputSeconds.textContent = '—';
outputMillis.textContent = '—';
outputISO.textContent = '—';
outputUTC.textContent = '—';
outputLocal.textContent = '—';
outputRelative.textContent = '—';
return;
}
const ms = date.getTime();
const sec = Math.floor(ms / 1000);
outputSeconds.textContent = sec;
outputMillis.textContent = ms;
outputISO.textContent = date.toISOString();
outputUTC.textContent = date.toUTCString();
outputLocal.textContent = date.toLocaleString('en-US', {
dateStyle: 'full',
timeStyle: 'long'
});
outputRelative.textContent = getRelativeTime(date);
}
function parseTimestamp(value) {
const num = parseInt(value, 10);
if (isNaN(num)) return null;
// If it's likely milliseconds (> year 3000 in seconds), treat as ms
if (num > 32503680000) {
return new Date(num);
}
// Otherwise treat as seconds
return new Date(num * 1000);
}
function fromTimestamp() {
if (updating) return;
updating = true;
const value = timestampInput.value.trim();
if (!value) {
updateOutputs(null);
dateInput.value = '';
timeInput.value = '';
updating = false;
return;
}
const date = parseTimestamp(value);
if (date && !isNaN(date.getTime())) {
// Update date/time inputs in selected timezone
const tz = timezoneSelect.value;
const formatter = new Intl.DateTimeFormat('en-CA', {
timeZone: tz,
year: 'numeric',
month: '2-digit',
day: '2-digit'
});
const timeFormatter = new Intl.DateTimeFormat('en-GB', {
timeZone: tz,
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
});
dateInput.value = formatter.format(date);
timeInput.value = timeFormatter.format(date);
updateOutputs(date);
} else {
updateOutputs(null);
}
updating = false;
}
function fromDateTime() {
if (updating) return;
updating = true;
const dateVal = dateInput.value;
const timeVal = timeInput.value || '00:00:00';
const tz = timezoneSelect.value;
if (!dateVal) {
timestampInput.value = '';
updateOutputs(null);
updating = false;
return;
}
try {
// Create date string and parse in the selected timezone
const dateTimeStr = `${dateVal}T${timeVal}`;
const date = new Date(new Date(dateTimeStr).toLocaleString('en-US', { timeZone: tz }));
// This is a bit hacky but works for timezone conversion
const localDate = new Date(dateTimeStr);
const utcDate = new Date(localDate.toLocaleString('en-US', { timeZone: 'UTC' }));
const tzDate = new Date(localDate.toLocaleString('en-US', { timeZone: tz }));
const offset = utcDate - tzDate;
const finalDate = new Date(localDate.getTime() + offset);
timestampInput.value = Math.floor(finalDate.getTime() / 1000);
updateOutputs(finalDate);
} catch (e) {
updateOutputs(null);
}
updating = false;
}
function updatePageURL() {
const ts = timestampInput.value.trim();
const newURL = new URL(window.location.href);
if (ts) newURL.searchParams.set('ts', ts);
else newURL.searchParams.delete('ts');
history.replaceState(null, '', newURL.toString());
}
timestampInput.addEventListener('input', () => { fromTimestamp(); updatePageURL(); });
dateInput.addEventListener('input', () => { fromDateTime(); updatePageURL(); });
timeInput.addEventListener('input', () => { fromDateTime(); updatePageURL(); });
timezoneSelect.addEventListener('change', () => { fromDateTime(); updatePageURL(); });
nowBtn.addEventListener('click', () => {
const now = Date.now();
timestampInput.value = Math.floor(now / 1000);
fromTimestamp();
updatePageURL();
});
// Load from URL or initialize with current time
const initialTs = new URLSearchParams(window.location.search).get('ts');
if (initialTs) {
timestampInput.value = initialTs;
fromTimestamp();
} else {
nowBtn.click();
}
// Click to copy functionality
document.querySelectorAll('.output-item[data-copy]').forEach(item => {
item.addEventListener('click', () => {
const valueEl = document.getElementById(item.dataset.copy);
const value = valueEl.textContent;
if (value === '—') return;
navigator.clipboard.writeText(value).then(() => {
item.classList.add('copied');
const hint = item.querySelector('.copy-hint');
const originalText = hint.textContent;
hint.textContent = 'Copied!';
hint.style.opacity = '1';
setTimeout(() => {
item.classList.remove('copied');
hint.textContent = originalText;
hint.style.opacity = '';
}, 1500);
});
});
});
</script>
</body>
</html>