PDA

View Full Version : Time of Day if statements


JNorth
04-26-2010, 10:51 PM
Hello

Im trying to get my 'Good Morning', "Good Afternoon' and 'Good evening' statements right on a webpage but when the clock turns past 6 pm, both the Good afternoon and Good evening texts display instead of just 'Good evening' alone. What am I doing wrong? See below:

/* <![CDATA[ */
// write good morning, good afternoon or good evening
var clientDate;
var hrs; //hours
var mins;// minutes
clientDate=new Date;
hrs= clientDate.getHours(); // get the hours component of the systems date/time
mins=clientDate.getMinutes();
if (hrs >=18 && mins >=0 && hrs)
document.write (" Good Evening, Welcome to Windsurf!"); //Say Good Evening
if (hrs >=12 && mins >=0 && hrs )
document.write (" Good Afternoon, Welcome to Windsurf!" ); //say Good afternoon
else
document.write(" Good Morning, Welcome to Windsurf!"); //Say Good Morning
/* ]]> */

qayyum4
04-27-2010, 05:29 AM
Hi JNorth,

Both your if statements will get executed at 18 hrs, and so both the messages will be displayed.

try this:
if (hrs >=18 && mins >=0)
document.write (" Good Evening, Welcome to Windsurf!"); //Say Good Evening
else if(hrs >=12 && mins >=0)
document.write (" Good Afternoon, Welcome to Windsurf!" ); //say Good afternoon
else
document.write(" Good Morning, Welcome to Windsurf!"); //Say Good Morning