-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimestamp.html
More file actions
609 lines (517 loc) · 19.3 KB
/
Copy pathtimestamp.html
File metadata and controls
609 lines (517 loc) · 19.3 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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>时间戳转换工具</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.navbar {
max-width: 1600px;
margin: 0 auto 20px;
background: white;
border-radius: 12px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
padding: 15px 30px;
display: flex;
align-items: center;
gap: 20px;
}
.navbar-title {
font-size: 20px;
font-weight: 700;
color: #667eea;
margin-right: auto;
}
.navbar-links {
display: flex;
gap: 10px;
}
.navbar-link {
padding: 10px 20px;
border-radius: 8px;
text-decoration: none;
color: #666;
font-weight: 600;
transition: all 0.3s;
font-size: 14px;
}
.navbar-link:hover {
background: #f5f5f5;
color: #667eea;
}
.navbar-link.active {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.container {
max-width: 1600px;
margin: 0 auto;
background: white;
border-radius: 12px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
overflow: hidden;
display: flex;
flex-direction: column;
min-height: calc(100vh - 40px);
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 30px;
text-align: center;
}
.header h1 {
font-size: 28px;
margin-bottom: 10px;
}
.header p {
font-size: 14px;
opacity: 0.9;
}
.content {
display: flex;
flex: 1;
min-height: 0;
}
.left-panel, .right-panel {
flex: 1;
padding: 30px;
display: flex;
flex-direction: column;
}
.left-panel {
border-right: 1px solid #eee;
}
.panel-title {
font-size: 18px;
font-weight: 600;
color: #333;
margin-bottom: 20px;
display: flex;
align-items: center;
gap: 10px;
}
.panel-title::before {
content: '';
width: 4px;
height: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 2px;
}
.input-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: #555;
font-size: 14px;
}
input[type="text"], input[type="datetime-local"] {
width: 100%;
padding: 12px;
border: 2px solid #e0e0e0;
border-radius: 8px;
font-size: 14px;
font-family: 'Monaco', 'Menlo', 'Courier New', monospace;
transition: border-color 0.3s;
}
input[type="text"]:focus, input[type="datetime-local"]:focus {
outline: none;
border-color: #667eea;
}
.button-group {
display: flex;
gap: 10px;
margin-bottom: 20px;
}
button {
padding: 12px 24px;
border: none;
border-radius: 8px;
font-size: 14px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
flex: 1;
}
.btn-primary {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.btn-primary:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}
.btn-secondary {
background: #f5f5f5;
color: #333;
}
.btn-secondary:hover {
background: #e0e0e0;
}
.result-box {
background: #f8f9fa;
border-radius: 8px;
padding: 20px;
min-height: 200px;
flex: 1;
display: flex;
flex-direction: column;
}
.result-item {
margin-bottom: 15px;
padding: 15px;
background: white;
border-radius: 6px;
border-left: 4px solid #667eea;
}
.result-label {
font-size: 12px;
color: #999;
margin-bottom: 8px;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.result-value {
font-size: 16px;
font-weight: 600;
color: #333;
font-family: 'Monaco', 'Menlo', 'Courier New', monospace;
word-break: break-all;
}
.error {
background: #ffebee;
color: #c62828;
padding: 15px;
border-radius: 8px;
border-left: 4px solid #c62828;
margin-top: 15px;
}
.info-box {
background: #e3f2fd;
padding: 15px;
border-radius: 8px;
margin-bottom: 20px;
border-left: 4px solid #2196f3;
font-size: 13px;
line-height: 1.6;
}
.info-box strong {
color: #1976d2;
}
.timestamp-options {
display: flex;
gap: 10px;
margin-bottom: 15px;
}
.option-btn {
flex: 1;
padding: 10px;
background: #f5f5f5;
border: 2px solid #e0e0e0;
border-radius: 8px;
cursor: pointer;
transition: all 0.3s;
font-size: 13px;
text-align: center;
}
.option-btn.active {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border-color: #667eea;
}
.current-time {
background: #f0f4ff;
padding: 15px;
border-radius: 8px;
margin-bottom: 20px;
text-align: center;
}
.current-time-label {
font-size: 12px;
color: #666;
margin-bottom: 5px;
}
.current-time-value {
font-size: 18px;
font-weight: 600;
color: #667eea;
font-family: 'Monaco', 'Menlo', 'Courier New', monospace;
}
@media (max-width: 768px) {
.navbar {
flex-direction: column;
gap: 15px;
padding: 15px 20px;
}
.navbar-title {
margin-right: 0;
}
.navbar-links {
width: 100%;
justify-content: center;
}
.content {
flex-direction: column;
}
.left-panel {
border-right: none;
border-bottom: 1px solid #eee;
}
}
</style>
</head>
<body>
<nav class="navbar">
<div class="navbar-title">🛠️ 开发工具集</div>
<div class="navbar-links">
<a href="decode.html" class="navbar-link">Base64 解码</a>
<a href="timestamp.html" class="navbar-link active">时间戳转换</a>
<a href="json-formatter.html" class="navbar-link">JSON 格式化</a>
<a href="json-diff.html" class="navbar-link">JSON Diff</a>
</div>
</nav>
<div class="container">
<div class="header">
<h2>⏰ 时间戳转换工具</h2>
</div>
<div class="current-time">
<div class="current-time-label">当前时间</div>
<div class="current-time-value" id="currentTime"></div>
<div class="current-time-label" style="margin-top: 5px;">当前时间戳(秒): <span id="currentTimestamp" style="font-family: 'Monaco', 'Menlo', 'Courier New', monospace;">-</span></div>
</div>
<div class="content">
<div class="left-panel">
<div class="panel-title">时间戳 → 可读时间</div>
<div class="input-group">
<label for="timestampInput">输入时间戳:</label>
<input type="text" id="timestampInput" placeholder="例如: 1609459200 或 1609459200000" />
</div>
<div class="button-group">
<button class="btn-primary" onclick="timestampToDate()">转换为时间</button>
<button class="btn-secondary" onclick="clearTimestamp()">清空</button>
</div>
<div class="result-box" id="timestampResult">
<div style="color: #999; text-align: center; padding: 40px;">
等待输入时间戳...
</div>
</div>
</div>
<div class="right-panel">
<div class="panel-title">可读时间 → 时间戳</div>
<div class="input-group">
<label for="dateInput">输入时间字符串:</label>
<input type="text" id="dateInput" placeholder="例如: 2024-01-01 12:00:00" />
</div>
<div class="input-group">
<label for="datePicker">或使用日期选择器:</label>
<input type="datetime-local" id="datePicker" />
</div>
<div class="button-group">
<button class="btn-primary" onclick="dateToTimestamp()">转换为时间戳</button>
<button class="btn-secondary" onclick="clearDate()">清空</button>
</div>
<div class="result-box" id="dateResult">
<div style="color: #999; text-align: center; padding: 40px;">
等待输入时间...
</div>
</div>
</div>
</div>
</div>
<script>
let timestampUnit = 'seconds'; // 'seconds' or 'milliseconds'
let outputUnit = 'seconds'; // 'seconds' or 'milliseconds'
// 更新当前时间
function updateCurrentTime() {
const now = new Date();
const timestamp = Math.floor(now.getTime() / 1000);
const timeStr = now.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
});
document.getElementById('currentTime').textContent = timeStr;
document.getElementById('currentTimestamp').textContent = timestamp;
}
// 每秒更新一次当前时间
updateCurrentTime();
setInterval(updateCurrentTime, 1000);
function setTimestampUnit(unit) {
timestampUnit = unit;
document.querySelectorAll('[data-unit]').forEach(btn => {
btn.classList.remove('active');
});
document.querySelector(`[data-unit="${unit}"]`).classList.add('active');
}
function setOutputUnit(unit) {
outputUnit = unit;
document.querySelectorAll('[data-output-unit]').forEach(btn => {
btn.classList.remove('active');
});
document.querySelector(`[data-output-unit="${unit}"]`).classList.add('active');
}
function timestampToDate() {
const input = document.getElementById('timestampInput').value.trim();
if (!input) {
showTimestampError('请输入时间戳');
return;
}
let timestamp = parseFloat(input);
if (isNaN(timestamp)) {
showTimestampError('时间戳格式错误,请输入数字');
return;
}
// 如果是秒级时间戳但输入的是毫秒级(大于 1e12),自动转换
if (timestampUnit === 'seconds' && timestamp > 1e12) {
timestamp = timestamp / 1000;
}
// 如果是毫秒级时间戳但输入的是秒级(小于 1e10),自动转换
else if (timestampUnit === 'milliseconds' && timestamp < 1e10) {
timestamp = timestamp * 1000;
}
// 转换为毫秒
const milliseconds = timestampUnit === 'seconds' ? timestamp * 1000 : timestamp;
const date = new Date(milliseconds);
if (isNaN(date.getTime())) {
showTimestampError('无效的时间戳');
return;
}
const result = formatDateResult(date, milliseconds);
document.getElementById('timestampResult').innerHTML = result;
}
function formatDateResult(date, milliseconds) {
const timestampSeconds = Math.floor(milliseconds / 1000);
const timestampMs = milliseconds;
const formats = [
{ label: '标准格式', value: date.toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }) },
{ label: 'ISO 8601', value: date.toISOString() },
{ label: 'UTC 时间', value: date.toUTCString() },
{ label: '年月日', value: date.toLocaleDateString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' }) },
{ label: '时分秒', value: date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }) },
{ label: '星期', value: date.toLocaleDateString('zh-CN', { weekday: 'long' }) },
{ label: '时间戳(秒)', value: timestampSeconds.toString() },
{ label: '时间戳(毫秒)', value: timestampMs.toString() },
];
let html = '';
formats.forEach(format => {
html += `
<div class="result-item">
<div class="result-label">${format.label}</div>
<div class="result-value">${format.value}</div>
</div>
`;
});
return html;
}
function dateToTimestamp() {
const textInput = document.getElementById('dateInput').value.trim();
const pickerInput = document.getElementById('datePicker').value;
let date;
// 优先使用日期选择器
if (pickerInput) {
date = new Date(pickerInput);
} else if (textInput) {
// 尝试解析文本输入
date = new Date(textInput);
} else {
showDateError('请输入时间或使用日期选择器');
return;
}
if (isNaN(date.getTime())) {
showDateError('无效的时间格式,请检查输入');
return;
}
const timestampSeconds = Math.floor(date.getTime() / 1000);
const timestampMs = date.getTime();
const result = formatTimestampResult(date, timestampSeconds, timestampMs);
document.getElementById('dateResult').innerHTML = result;
}
function formatTimestampResult(date, timestampSeconds, timestampMs) {
const formats = [
{ label: '时间戳(秒)', value: timestampSeconds.toString() },
{ label: '时间戳(毫秒)', value: timestampMs.toString() },
{ label: '标准格式', value: date.toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }) },
{ label: 'ISO 8601', value: date.toISOString() },
{ label: 'UTC 时间', value: date.toUTCString() },
];
let html = '';
formats.forEach(format => {
html += `
<div class="result-item">
<div class="result-label">${format.label}</div>
<div class="result-value">${format.value}</div>
</div>
`;
});
return html;
}
function showTimestampError(message) {
document.getElementById('timestampResult').innerHTML = `<div class="error">${escapeHtml(message)}</div>`;
}
function showDateError(message) {
document.getElementById('dateResult').innerHTML = `<div class="error">${escapeHtml(message)}</div>`;
}
function clearTimestamp() {
document.getElementById('timestampInput').value = '';
document.getElementById('timestampResult').innerHTML = '<div style="color: #999; text-align: center; padding: 40px;">等待输入时间戳...</div>';
}
function clearDate() {
document.getElementById('dateInput').value = '';
document.getElementById('datePicker').value = '';
document.getElementById('dateResult').innerHTML = '<div style="color: #999; text-align: center; padding: 40px;">等待输入时间...</div>';
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// 日期选择器变化时自动转换
document.getElementById('datePicker').addEventListener('change', function() {
if (this.value) {
dateToTimestamp();
}
});
// 支持 Enter 键快捷转换
document.getElementById('timestampInput').addEventListener('keydown', function(e) {
if (e.key === 'Enter') {
timestampToDate();
}
});
document.getElementById('dateInput').addEventListener('keydown', function(e) {
if (e.key === 'Enter') {
dateToTimestamp();
}
});
// 设置日期选择器的默认值为当前时间
function setDefaultDateTime() {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const datetimeLocal = `${year}-${month}-${day}T${hours}:${minutes}`;
document.getElementById('datePicker').value = datetimeLocal;
}
setDefaultDateTime();
</script>
</body>
</html>