Working with Days

You might want to display a different message to your users if it’s a weekend. Script 12.2 tells you how to do it.

Figure 12.2. The happy news gets written to the window.

To figure out if it is a weekend:

1.
var now = new Date();
Fill the variable now with the current date.
2.
if (now.getDay() > 0 && now.getDay() < 6) {
This extracts the numerical day of the week from the now variable and asks if it is greater than 0 (remember that Sunday is 0). Next the line uses the && operator, which is a logical and (i.e., both parts have to be true), and asks if now is less than 6, which is the number for Saturday.
3.
dtString = "Sorry, ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.