Nanfeng

Notes on software development, code, and curious ideas

Building a Romantic Relationship Timer Web Page

Relationship timer preview

Demo: open the relationship timer.

The page below calculates elapsed days, hours, minutes, and seconds from a chosen date. Replace the date and message with your own values.

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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Our Time Together</title>
<style>
body {
min-height: 100vh; margin: 0; display: grid; place-items: center;
color: #5b3155; text-align: center; font-family: system-ui, sans-serif;
background: linear-gradient(145deg, #fce8f8, #ffe8ed);
}
main { padding: 2rem; border-radius: 1.5rem; background: #fff9; }
#timer { font-size: clamp(1.2rem, 4vw, 2rem); font-weight: 700; }
</style>
</head>
<body>
<main>
<h1>Our story continues</h1>
<p>We have been together for</p>
<p id="timer" aria-live="polite"></p>
</main>
<script>
const startedAt = new Date("2023-01-01T00:00:00");
const timer = document.querySelector("#timer");

function updateTimer() {
const total = Math.max(0, Date.now() - startedAt.getTime());
const seconds = Math.floor(total / 1000);
const days = Math.floor(seconds / 86400);
const hours = Math.floor(seconds / 3600) % 24;
const minutes = Math.floor(seconds / 60) % 60;
const rest = seconds % 60;
timer.textContent = `${days} days ${hours} hours ${minutes} minutes ${rest} seconds`;
}
updateTimer();
setInterval(updateTimer, 1000);
</script>
</body>
</html>

Save the code as an HTML file and open it locally. For a public page, use only photos and personal details that everyone shown has agreed to publish. ISO timestamps with an explicit time-zone offset are preferable when visitors may open the page in different regions.

+