• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

15. Dynamic Content Applications > 15.6. Greeting Users with Their Time of Day

Greeting Users with Their Time of Day

NN 2, IE 3

Problem

You want your page to include a greeting pertinent to the user’s part of the day, such as “Good morning” or “Good afternoon”.

Solution

First, create a function that returns strings associated with each day part, as calculated by a fresh Date object:

function dayPart( ) {
    var oneDate = new Date( );
    var theHour = oneDate.getHours( );
    if (theHour < 12) {
        return "morning";
    } else if (theHour < 18) {
        return "afternoon";
    } else {
        return "evening";
    }
}

To accommodate both scriptable and unscriptable browsers, be sure to encase the script statement inside HTML comment tags, and include the noscript element with the text to display for unscriptable browsers.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint