Time of Day

This very simple script determines what time of day it is and runs a function or script based on the information.  The example below is the same as the script on our home page.  Depending on the time, the message below will change. 


<script language="JavaScript">
<!--
var TodaysDate = new Date( )
    var Hours = TodaysDate.getHours( )
    if (Hours < 12 ) {
        document.write("Good Morning!")
        }
    else {
        if (Hours < 18) {
            document.write("Good Afternoon!")
            }
        else {
            document.write("Good Evening!")
        }   
    }
//-->
</script>


  • Operating systems will return the time as military time, meaning anything past 12 noon continues on to 13, 14, etc which represents 1pm, 2pm, respectively. Remember that when determining your parameters
  • The alert messages listed in red, as well as the if / else statements can be changed however you see fit.  You can use nested if statements to provide better control over the results.
  • Once the parameters have been satisfied, you can automatically write messages like we've done here, redirect the user, or run preset functions (e.g., a person visiting your site in the morning would be redirected to "morning.htm")

Back to Web Tips