Skip to main content

JavaScript - Simple Time Based Greetings


In this tutorial we will create a Simple Time Based Greetings using JavaScript. This code will automatically display a greeting whenever the page start. You can use this script in your system to make your client smile in a good way. This program is a user-friendly feel free to use it in your system.

We will use JavaScript to allow you to implement complex things on web pages. It enables you to create dynamically updating content and add some interactive visual for the user transactions.

Getting Started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">JavaScript - Simple Time Based Greetings</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div id="greeting"></div>
  17. </div>
  18. <script src="js/script.js"></script>
  19. </body>
  20. </html>

Creating the Script

This code contains the script of the application. This code will display a greeting based on your computer time. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory

  1. var date = new Date();
  2. var hours = date.getHours();
  3. var greet;
  4.  
  5. if(hours < 12){
  6. greet = "<center><img src='images/image1.png' width='500'/></center>";
  7. }else if(hours >= 12 && hours <= 17){
  8. greet = "<center><img src='images/image2.png' width='500'/></center>";
  9. }else if(hours >= 17 && hours <= 24){
  10. greet = "<center><img src='images/image3.png' width='500'/></center>";
  11. }
  12.  
  13. document.getElementById('greeting').innerHTML = greet;

There you have it we successfully created a Simple Time Based Greetings using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.