Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org

Related items

JavaScript Y2K Issues

And now...The Weekly Update Script

The 24 Hour World

Today's The Day

Extending "Born of the 4th of July"

Easter

Born on the 4th of July

The Chinese New Year

What sign are you?

Monday's child is full of grace

The 3rd Saturday in November

You are here: irt.org | Articles | JavaScript | Date and Time | The 3rd Saturday in November [ previous next ]

Published on: Saturday 22nd November 1997 By: Martin Webb

Introduction

This article will describe how to calculate the 1st, 2nd, 3rd, 4th, 5th and last weekdays of any month. This will then be used to calculate the dates of various variable Calendar Events or Holidays.

The nth day of the month

Using the DayOfWeek() function described in the previous article Born on the 4th of July we can create the following NthDay() function to return the date of the nth weekday within the month of the year:

function NthDay(nth,weekday,month,year) {
    return (nth-1)*7 + 1 + (7 + weekday - DayOfWeek((nth-1)*7 + 1,month,year))%7;
}

For example:

document.write(NthDay(3,7,11,1997)+'<BR>');

When run produces the date of the 3rd Saturday in November for 1997:

The last day of the month

Using the makeArray() and LeapYear() functions and the daysofmonth[] and daysofmonthLY[] arrays, described in the previous article Blind Date, using the number of days within the month we can find the date of the last weekday within the month of the year using the LastDay() function:

function LastDay(weekday,month,year) {
    if (LeapYear(year)) var days = daysofmonthLY[month];
    else                var days = daysofmonth[month];

    return days - (DayOfWeek(days,month,year) - weekday + 7)%7;
}

For example the following script will show the dates of the last Sunday through to Saturday of December 1997:

for (var i=1; i<8; i++)
    document.write(LastDay(i,12,1997)+' ');

When run it produces:

Variable Calendar Events/Holidays

Along with the events already described in the previous article Born on the 4th of July, the following events occur on different days of the month depending on the year:

So for example, to calculate the date that Martin Luther King Day falls on in 1998 we could use:

document.write("Martin Luther King Day - " + NthDay(3,2,1,1998));

Which when run produces:

However, with just a few variable definitions:

var sun=1,mon=2,tue=3,wed=4,thu=5,fri=6,sat=7;
var jan=1,feb=2,mar=3,apr=4,may=5,jun=6,jul=7,aug=8,sep=9,oct=10,nov=11,dec=12;
var first=1,second=2,third=3,fourth=4,fifth=5,last=-1;

and a slight tinkering of the NthDay() function we can make life really simple:

function NthDay(nth,weekday,month,year) {
    if (nth > 0) return (nth-1)*7 + 1 + (7 + weekday - DayOfWeek((nth-1)*7 + 1,month,year))%7;
    if (LeapYear(year)) var days = daysofmonthLY[month];
    else                var days = daysofmonth[month];
    return days - (DayOfWeek(days,month,year) - weekday + 7)%7;
}

We can now call the NthDay() function to compute the first, second, third, fourth, fifth and last weekday, making the LastDate() function obsolete. Along with the FullDate() function, described in the previous article Monday's child is full of grace, we can calculate the dates as follows:

document.write("Daylight Savings time begins - " + FullDate(NthDay(first,sun,apr,1997),apr,1997) + '<BR>');
document.write("Mother's Day - " + FullDate(NthDay(second,sun,may,1997),may,1997) + '<BR>');
document.write("Armed Forces Day - " + FullDate(NthDay(third,sat,may,1997),may,1997) + '<BR>');
document.write("Thanksgiving Day - " + FullDate(NthDay(fourth,thu,nov,1997),nov,1997) + '<BR>');
document.write("5th Wednesday in December 1997 - " + FullDate(NthDay(fifth,wed,dec,1997),dec,1997) + '<BR>');
document.write("Daylight Savings Time ends - " + FullDate(NthDay(last,sun,oct,1997),oct,1997) + '<BR>');

Which when run produces:

Working Example

Try the frame version which shows all the variable calendar events and holidays for the current year:

Source Code

You can view the source code of the four components:

Related items

JavaScript Y2K Issues

And now...The Weekly Update Script

The 24 Hour World

Today's The Day

Extending "Born of the 4th of July"

Easter

Born on the 4th of July

The Chinese New Year

What sign are you?

Monday's child is full of grace

©2018 Martin Webb