-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
115 lines (88 loc) · 2.62 KB
/
index.html
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
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<title>parse date time strings to readable UTC & local time</title>
<style>
body {
padding: 20px;
margin-bottom: 20px;
}
main {
padding-left: 20px;
}
.container {
padding-top: 100px;
}
label {
font-size: 1.5rem;
}
input {
width: 20vw;
min-width: 150px;
}
h1 {
margin-bottom: 4px;
}
h3 {
font-weight: 500;
margin-bottom: 4px;
}
.subsection {
margin-left: 10px;
}
footer {
padding-top: 100px;
}
</style>
</head>
<body>
<h1>parse date time strings (such as ISO 8601) to UTC & local time</h1>
<main>
<p>We do not record anything. All logic is done on the client side.</p>
<div class="container">
<label for="input">Input your date time</label><br />
<input type="text" id="input" class="subsection" name="input" oninput="parseInput()" />
<p>
<h3>UTC</h3>
<div id="utc" class="subsection">N/A</div>
</p>
<p>
<h3>Local</h3>
<div id="local" class="subsection">N/A</div>
</p>
</div>
</main>
<footer>
<a href="https://rexfeng.com">xta</a> 2024 <a href="https://github.com/xta/js-parse-date-time-string">view on
github</a>
<p>
<a href="https://xta.github.io/am-pm-visualized/">12 PM is noon, 12 AM is midnight</a>
</p>
</footer>
</div>
</body>
<script>
function parseInput() {
var input = document.getElementById('input').value;
var date = new Date(Date.parse(input));
// set UTC string
var utcEl = document.getElementById('utc');
utcEl.innerText = date.toUTCString();
// set local string
var outputEl = document.getElementById('local');
var outputWeekday = date.toLocaleString('en-us', { weekday: 'short' })
var formatter = Intl.DateTimeFormat(undefined, { timeZoneName: 'long' });
var timezone = formatter.formatToParts(date).find((part) => part.type == 'timeZoneName').value;
var output = `${outputWeekday}, ${date.toLocaleString()} ${timezone}`
outputEl.innerText = output;
}
function setupInput() {
var inputEl = document.getElementById('input');
var now = new Date()
inputEl.value = now.toISOString()
}
setupInput()
parseInput()
</script>
</html>