Skip to content

Commit 826c496

Browse files
committed
Update time right away
1 parent 2889b25 commit 826c496

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed
File renamed without changes.

public/js/update-time.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Included directly in the browser, hence why this can't be TS
2+
const datetime = document.querySelector('time')
3+
const readableTime = document.querySelector('.js-time-of-day')
4+
5+
function updateTime() {
6+
if (!datetime) return
7+
8+
const today = new Date()
9+
const hour = today.getHours()
10+
const hourFormatted = hour < 10 ? '0' + hour : hour
11+
const min = today.getMinutes()
12+
const minFormatted = min < 10 ? '0' + min : min
13+
let timeOfDay = hour < 12 ? 'morning' : hour < 18 ? 'afternoon' : 'evening'
14+
15+
if (hour < 6) timeOfDay = 'night'
16+
17+
const time = `${hourFormatted}:${minFormatted}`
18+
datetime.setAttribute('datetime', today.toISOString())
19+
datetime.textContent = time
20+
readableTime.textContent = timeOfDay
21+
}
22+
23+
updateTime()
24+
setInterval(updateTime, 30 * 1000)

src/components/BaseHead.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ const { title, description, image = '/blog-placeholder.jpg' } = Astro.props
4242
<meta property="twitter:description" content={description} />
4343
<meta property="twitter:image" content={new URL(image, Astro.url)} />
4444

45-
<script is:inline src="/sw-destroy.js"></script>
45+
<script is:inline src="/js/sw-destroy.js"></script>

src/components/Footer.astro

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ if (hour < 6) timeOfDay = 'night'
1111

1212
<footer>
1313
<hr />
14-
<p>Good {timeOfDay}! It's <time :datetime={today}>{hourFormatted}:{minFormatted}</time>.</p>
14+
<p>
15+
Good <span class="js-time-of-day">{timeOfDay}</span>! It's <time :datetime={today}
16+
>{hourFormatted}:{minFormatted}</time
17+
>.
18+
</p>
1519

1620
<div class="links">
1721
<a href="mailto:[email protected]"> E-mail me! Write about anything.</a>
@@ -27,29 +31,7 @@ if (hour < 6) timeOfDay = 'night'
2731
</div>
2832
</footer>
2933

30-
<script type="module">
31-
const datetime = document.querySelector('time')
32-
33-
function updateTime() {
34-
if (!datetime) return
35-
36-
const today = new Date()
37-
const hour = today.getHours()
38-
const hourFormatted = hour < 10 ? '0' + hour : hour
39-
const min = today.getMinutes()
40-
const minFormatted = min < 10 ? '0' + min : min
41-
let timeOfDay = hour < 12 ? 'morning' : hour < 18 ? 'afternoon' : 'evening'
42-
43-
if (hour < 6) timeOfDay = 'night'
44-
45-
const time = `${hourFormatted}:${minFormatted}`
46-
const greeting = `Good ${timeOfDay}! It's ${time}.`
47-
datetime.textContent = greeting
48-
datetime.setAttribute('datetime', today.toISOString())
49-
}
50-
51-
setInterval(updateTime, 30 * 1000)
52-
</script>
34+
<script is:inline type="module" src="/js/update-time.js"></script>
5335

5436
<style>
5537
footer {

0 commit comments

Comments
 (0)