How To Create Greetings Message Using JavaScript

In this tutorial, we are going to learn how to create Greetings Message using JavaScript. We learn on how to display greetings message according to the time of the day using JavaScript source code. Kindly copy and paste the source code below to the BODY tag of your page.

JavaScript Source Code

This script will display the greetings message using the time of the day. Kindly copy and paste the source code below to the BODY tag of your page.
  1. <center>
  2. <h2 style="color:blue;">
  3. How To Create Greetings Message Using JavaScript
  4. </h2>
  5.  
  6. <script>
  7. var Time_of_The_Day=new Date()
  8. var hours=Time_of_The_Day.getHours()
  9.  
  10. if (hours>=5&&hours<=11)
  11. //GREETING MESSAGE FOR MORNING
  12. document.write('<h2>Hello! Good Morning! Have a nice day.</h2>')
  13. else if (hours==12)
  14. //GREETING MESSAGE FOR NOON
  15. document.write('<h2>Good Noon Visitor! </h2>')
  16. else if (hours>=13&&hours<=17)
  17. //GREETING MESSAGE FOR AFTERNOON
  18. document.write('Example:<h2 style="color:#1982d1;">Good Afternoon! </h2>')
  19. else if (hours>=18&&hours<=20)
  20. //MESSAGE FOR EVENING (6pm-8pm)
  21. document.write('<h2>Good Evening! </h2>')
  22. else if (hours>=21&&hours<=11)
  23. //GREETING MESSAGE FOR NIGHT (9pm-11pm)
  24. document.write('<h2>Good Night! </h2>')
  25. else
  26. //GREETING MESSAGE FOR LATE NIGHT (12pm-4am)
  27. document.write('<h2>Wow! You`re still awake. Working Late?</h2>')
  28.  
  29. </script>
  30.  
  31. </center>

This is the result of the source code above:

Result So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment