softare development

Greet your Website Visitors based On the time of the Day

JavaScript allows us to dynamically change greetings based on the current hour of the day, providing a personalized touch to web applications. In this article, we’ll walk through a step-by-step guide on creating a simple JavaScript function that changes greetings according to morning, afternoon, and evening.

Step 1: Set Up HTML Structure

Begin by creating a basic HTML structure with a placeholder for the greeting text:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dynamic Greetings</title>
</head>
<body>
    <div id="greeting-container">
        <p id="greeting-text">Good Morning!</p>
    </div>
    <script src="script.js"></script>
</body>
</html>

Step 2: Write JavaScript Logic

Now, create a JavaScript file (script.js) and add the logic to change greetings based on the current hour:

document.addEventListener('DOMContentLoaded', function () {
    const greetingText = document.getElementById('greeting-text');

    function getCurrentGreeting() {
        const currentHour = new Date().getHours();

        if (currentHour >= 5 && currentHour < 12) {
            return 'Good Morning!';
        } else if (currentHour >= 12 && currentHour < 18) {
            return 'Good Afternoon!';
        } else {
            return 'Good Evening!';
        }
    }

    function updateGreeting() {
        const greeting = getCurrentGreeting();
        greetingText.textContent = greeting;
    }

    // Update greeting initially
    updateGreeting();

    // Update greeting every minute
    setInterval(updateGreeting, 60000);
});

This JavaScript code defines a function getCurrentGreeting() that determines the current time and returns the appropriate greeting. The updateGreeting() function then updates the displayed greeting text on the webpage.

The setInterval method ensures that the greeting is updated every minute to reflect real-time changes.

Step 3: Style Your Greeting

Style your greeting to make it visually appealing. You can add some CSS to center the text and make it visually attractive:

body {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    margin: 0;
    background-color: #f0f0f0;
}

#greeting-container {
    text-align: center;
}

#greeting-text {
    font-size: 24px;
    font-weight: bold;
    color: #333;
}

Step 4: Test Your Application

Open your HTML file in a web browser and observe the greeting text change dynamically based on the current hour. The application will greet the user with “Good Morning” in the morning, “Good Afternoon” in the afternoon, and “Good Evening” in the evening.

Certainly! Here’s the complete HTML file that integrates the HTML structure, JavaScript logic, and CSS styling:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dynamic Greetings</title>
    <style>
        body {
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
        }

        #greeting-container {
            text-align: center;
        }

        #greeting-text {
            font-size: 24px;
            font-weight: bold;
            color: #333;
        }
    </style>
</head>
<body>
    <div id="greeting-container">
        <p id="greeting-text">Good Morning!</p>
    </div>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const greetingText = document.getElementById('greeting-text');

            function getCurrentGreeting() {
                const currentHour = new Date().getHours();

                if (currentHour >= 5 && currentHour < 12) {
                    return 'Good Morning!';
                } else if (currentHour >= 12 && currentHour < 18) {
                    return 'Good Afternoon!';
                } else {
                    return 'Good Evening!';
                }
            }

            function updateGreeting() {
                const greeting = getCurrentGreeting();
                greetingText.textContent = greeting;
            }

            // Update greeting initially
            updateGreeting();

            // Update greeting every minute
            setInterval(updateGreeting, 60000);
        });
    </script>
</body>
</html>

Conclusion

Creating a JavaScript current hour change for greetings adds a personal and dynamic touch to your web applications. This simple script enhances user experience by adapting the displayed content to the time of day. Feel free to customize the greetings and styling to suit your application’s theme and requirements.

CSS Flex-Box

Recent Posts

AWS Expands Payment Options for Nigerian Customers, Introducing Naira (NGN) for Local Transactions

Amazon Web Services (AWS) continues to enhance its customer experience by offering more flexible payment…

2 days ago

Why JavaScript Remains Dominant in 2025

JavaScript, often hailed as the "language of the web," continues to dominate the programming landscape…

4 days ago

Amazon Moves to Upgrade Alexa with Generative AI Technology

Amazon is accelerating efforts to reinvent Alexa as a generative AI-powered “agent” capable of performing…

4 days ago

Smuggled Starlink Devices Allegedly Used to Bypass India’s Internet Shutdown

SpaceX's satellite-based Starlink, which is currently unlicensed for use in India, is reportedly being utilized…

5 days ago

Why Netflix Dumped React For its Frontend

Netflix, a pioneer in the streaming industry, has always been at the forefront of adopting…

5 days ago

Microsoft Files Lawsuit Against Hacking Group Misusing Azure AI for Malicious Content Generation

Microsoft has announced legal action against a 'foreign-based threat actor group' accused of running a…

1 week ago