Display Greeting Message According to the Time using JavaScript

Display Greeting Message According to the Time using JavaScript

Display Greeting Message According to the Time using JavaScript

Introduction

Hello, today we’re going to learn how to use HTML, CSS & JavaScript to create a Display Greeting Message According to the Time. By following these instructions, you can simply make this Display Greeting Message According to the Time in HTML, CSS & JavaScript. Simply by adhering to the procedures mentioned below, you will be able to develop this amazing Display Greeting Message According to the Time.

Display Greeting Message According to the Time using JavaScript

This project will is good for beginners and help them to build their front-end development skills. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Time Based Dynamic Greetings Project.

Project Description

Step 1
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Time Based Dynamic Greetings Project.

Step 2
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Time Based Dynamic Greetings Project.

10+ Javascript Projects For Beginners With Source Code

Step 3
At last we will use JS (JavaScript) which will add a logic to make the Time Based Dynamic Greetings Project functioning from the user end.

I hope you have got an idea about the project.

HTML Code for Time Based Dynamic Greetings

<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Dynamic Greetings</title>
    <!-- Google Fonts -->
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins&display=swap"
      rel="stylesheet"
    />
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">DEMO TEXT</div>
    <!-- Script -->
    <script src="script.js"></script>
  </body>
</html>

First we’ll start with creating the structure of the Time Based Dynamic Greetings project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.

CSS Code for Time Based Dynamic Greetings

Ecommerce Website Using HTML, CSS, & JavaScript (Source Code)

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
body {
  background-color: #ae9cff;
}
.container {
  font-size: 1.3em;
  width: 25em;
  background: linear-gradient(-240deg, #8468ff 70%, #785bf7 70%);
  position: absolute;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  padding: 4.5em 1.3em;
  border-radius: 0.4em;
  box-shadow: 0 1.3em 1.8em rgba(94, 72, 181, 0.25);
}
.container h1 {
  font-family: "Poppins", sans-serif;
  text-align: center;
  color: #ffffff;
  text-transform: uppercase;
  font-weight: 400;
  letter-spacing: 0.1em;
  word-spacing: 0.4em;
}

Second comes the CSS code, which is mentioned above in that we have styled for the structure we have padded as well as aligned the Time Based Dynamic Greetings project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now we have created the structure using HTML and styled the webpage using CSS its time to add the functionality using JavaScript in this project.

JavaScript Code for Time Based Dynamic Greetings

let container = document.querySelector(".container");
let timeNow = new Date().getHours();
let greeting =
  timeNow >= 5 && timeNow < 12
    ? "Good Morning"
    : timeNow >= 12 && timeNow < 18
    ? "Good Afternoon"
    : "Good evening";
container.innerHTML = `<h1>${greeting}</h1>`;

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. In this code we have set the range of time so that it knows how to greet the user according to the current time. Let us see the Final Output of the project Time Based Dynamic Greetings using HTML, CSS & JavaScript (Source Code).

100+ JavaScript Projects With Source Code ( Beginners to Advanced)

Output

Live Preview of Display Greeting Message According to the Time

See the Pen
Dynamic Greetings
by Harsh Sawant (@harshh9)
on CodePen.

We have Successfully created our Time Based Dynamic Greetings using HTML, CSS & JavaScript. You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9



Leave a Reply