Planning greeting functionality
I'm gathering my thoughts about how to create a real-time greeting based on the current time. I'll use new Date() and padStart, plus I can consider inline CSS and JS for simplicity. It's essential to make the script run after the DOM loads, maybe by placing it just before the </body> tag. The time ranges will guide the greeting: "Good Morning," "Good Afternoon," "Good Evening," or "Good Night." I'll include a user-friendly implementation that runs continuously with setInterval. Let's keep it straightforward!Creating HTML example
I want to provide a straightforward HTML example for the user. I’ll emphasize saving the code as index.html and suggest opening it in a browser. I need to avoid any unnecessary phrases like "Great question" and dive straight into the content. My answer will include a complete minimal example with HTML structure and CSS for styling. I also want to consider using setInterval cautiously to calculate the date and update both the time and greeting fluidly. Let’s move forward with this! Here’s a complete, simple example of a real-time clock with a dynamic greeting that changes based on the time of day.
You can put everything below into a single index.html file and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Real-Time Clock with Greeting</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
Roboto, sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
background: linear-gradient(135deg, #1e3c72, #2a5298);
color: #fff;
}
.clock-container {
text-align: center;
padding: 2rem 3rem;
border-radius: 1rem;
background: rgba(0, 0, 0, 0.35);
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.4);
backdrop-filter: blur(10px);
min-width: 260px;
}
.time {
font-size: 3rem;
font-weight: 600;
letter-spacing: 0.1em;
margin-bottom: 0.5rem;
}
.date {
font-size: 1rem;
opacity: 0.85;
margin-bottom: 1.5rem;
}
.greeting {
font-size: 1.4rem;
font-weight: 500;
}
/* Optional: slightly different color tints for times of day */
body.morning {
background: linear-gradient(135deg, #ffb347, #ffcc33);
}
body.afternoon {
background: linear-gradient(135deg, #56ccf2, #2f80ed);
}
body.evening {
background: linear-gradient(135deg, #0f2027, #203a43, #2c5364);
}
body.night {
background: radial-gradient(circle at top, #283e51, #0a2342);
}
</style>
</head>
<body>
<div class="clock-container">
<div class="time" id="time">--:--:--</div>
<div class="date" id="date">Loading date...</div>
<div class="greeting" id="greeting">Hello!</div>
</div>
<script>
function pad(number) {
return String(number).padStart(2, "0");
}
function updateClock() {
const now = new Date();
let hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();
// 12-hour format
const amPm = hours >= 12 ? "PM" : "AM";
const twelveHour = hours % 12 || 12;
// Update time text
const timeString = `${pad(twelveHour)}:${pad(minutes)}:${pad(seconds)} ${amPm}`;
document.getElementById("time").textContent = timeString;
// Update date text
const options = { weekday: "long", year: "numeric", month: "long", day: "numeric" };
const dateString = now.toLocaleDateString(undefined, options);
document.getElementById("date")..textContent = dateString;
// Determine greeting based on hour (24-hour clock)
let greetingText = "";
let bodyClass = "";
if (hours >= 5 && hours < 12) {
greetingText = "Good Morning!";
bodyClass = "morning";
} else if (hours >= 12 && hours < 17) {
greetingText = "Good Afternoon!";
bodyClass = "afternoon";
} else if (hours >= 17 && hours < 21) {
greetingText = "Good Evening!";
bodyClass = "evening";
} else {
greetingText = "Good Night!";
bodyClass = "night";
}
document.getElementById("greeting").textContent = greetingText;
// Update body background class
document.body.classList.remove("morning", "afternoon", "evening", "night");
document.body.classList.add(bodyClass);
}
// Initial call
updateClock();
// Update every second
setInterval(updateClock, 1000);
</script>
</body>
</html>
If you’d like, I can also show how to separate this into three files (index.html, style.css, script.js) and link them together.