Good Morning, Afternoon, and Evening by Brad Jones | JavaScriptSource

Good Morning, Afternoon, and Evening by Brad Jones

Brad Jones Mar 19, 2012

Abstract

Adjust your message based on the time. See an example of saying Good Morning, Good Afternoon, or Good Evening based on the time of the day.

Description

Adjust your message based on the time. See an example of saying Good Morning, Good Afternoon, or Good Evening based on the time of the day. You'll be able to adjust the times in the script to fit your need. For example, you can use the same information to determine if the time of day is business hours (e.g. between 9 and 5 pm).

Code Snippet

<html> <header> <title>My test page </title> </header>  <body> <h1>Adjusting Your Message Based on the Time of Day:</h1> <h2>********************</h2>  Welcome to my page. I just wanted to say:   <script language="JavaScript"> var myDate = new Date();   /* hour is before noon */ if ( myDate.getHours() < 12 )  {     document.write("Good Morning!"); } else  /* Hour is from noon to 5pm (actually to 5:59 pm) */ if ( myDate.getHours() >= 12 && myDate.getHours() <= 17 ) {     document.write("Good Afternoon!"); } else  /* the hour is after 5pm, so it is between 6pm and midnight */ if ( myDate.getHours() > 17 && myDate.getHours() <= 24 ) {     document.write("Good Evening!"); } else  /* the hour is not between 0 and 24, so something is wrong */ {     document.write("I'm not sure what time it is!"); }  /* remove the following in your live script. It is for testing */ document.write("<br /><br /> The hour is: ") document.write( myDate.getHours() );  </script>    <h2>********************</h2>  </body> </html>

Download

Download

Leave a Response

(0 comments)