Two Hoots Banner



Javascript to display welcome message on home page.

The welcome message on my homepage is split into two javascripts. The first one displays the welcome message, i.e. "Good morning" accordingly to the current time, giving a good morning, afternoon or evening text.

// Function - Welcome()
// Used to return a welcome message depending on the current time	
function Welcome(){
 // Declare local parameters
 var now;
 var hour;
 // Get the current Date and Time
 now = new Date();
 // Get the Hours field of the Current Date Object
 hour = now.getHours();
 // Set the greeting message depending on the current hour
 if (hour > 0 && hour < 12){
 return "Good morning";
 }
else if (hour > 11 && hour <18){
 return "Good afternoon";
 }
else 
 return "Good evening";
 }
The second function displays the current date and time.

// Function - DisplayDateTime
// Description - Displays Current Date and Time on the Home Page
function DisplayDateTime(){
 // Create array of Days of the Week
 var dayarray = new Array("Sunday","Monday","Tuesday",
                          "Wednesday","Thursday","Friday","Saturday");
 // Create array of Months of the year
 var montharray = new Array("January","February","March","April",
                            "May","June","July","August","September",
                            "October","November","December");
 // Set now as a Date object to current date and time
 var now = new Date();
 // Get the year of the now date object
 var myyear = now.getFullYear();
 // Get the month of the now date object
 var mymonth = now.getMonth()
 // Get the day of the now date object
 var myday = now.getDay();
 // Get the day of the month of the now date object
 var mydate = now.getDate();
 // Get the hours of the now date object
 var myhour = now.getHours();
 // Get the minutes of the now date object
 var myminute = now.getMinutes();
 // Get the seconds of the now date object
 var mysecond = now.getSeconds();
 // Overcome bug where IE 3 returns the year minus 1900, and where Netscape 2 
 // and 3 returns year minus 1900 for dates between 1900 and 1999
 if (myyear<1000){myyear+=1900};
 // Set midday to either "am" or "pm"
 if (myhour>=12){mymidday="pm"} else {mymidday="am"}
 // Set hours from 1 to 12 and not 24 hour clock
 if (myhour>12){myhour=myhour-12};
 // If zero then must have been 12
 if (myhour==0){myhour=12};
 // Add zero to hour if less than 10 to get "03" for example
 if (myhour<10){myhour="0"+myhour};
 // Add zero to minute if less than 10 to get "03" for example
 if (myminute<10){myminute="0"+myminute};
 // Add zero to second of less than 10 to get "03" for example
 if (mysecond<10){mysecond="0"+mysecond};
 // If date is 1,21 or 31 set date identifier to be st, e.g. 1st - put st as superscript 
 if (mydate==1 || mydate==21 || mydate==31){ender="<sup>st</sup>"}
 // If date is 2 or 22 set date identifier to be nd, e.g. 2nd - put nd as superscript
 if (mydate==2 || mydate==22){ender="<sup>nd</sup>"}
 // If date is 3 or 23 set date identifier to be rd, e.g. 3rd - put rd as superscript
 if (mydate==3 || mydate==23){ender="<sup>rd</sup>"}
 // All others set date identifier to be th, e.g. 5th - put th as superscript
 else {ender="<sup>th</sup>"} 
 // Create variable for return value
 var timetext; 
 // Add Day of the week
 timetext = dayarray[myday];
 // Add Month
 timetext+=", " + montharray[mymonth];
 // Add day and date identifier
 timetext+=" " + mydate+ender;
 // Add year
 timetext+=" " + myyear;
 // Add Hour
 timetext+=", " + myhour;
 // Add Minute
 timetext+=":" + myminute;
 // Add Second
 timetext+=":" + mysecond;
 // Add am/pm
 timetext+=mymidday;
 // Return text
 return timetext;
}
Giving :-