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

Q613 How can I automatically calculate the date of the next meeting which is held on the second Thursday of each month?

You are here: irt.org | FAQ | JavaScript | Date | Q613 [ previous next ]

Try:

<HTML>
<HEAD>

<SCRIPT LANGUAGE="JavaScript"><!--
function y2k(number) {
    return (number < 1000) ? number + 1900 : number;
}

function DayOfWeek(day,month,year) {
    var a = Math.floor((14 - month)/12);
    var y = year - a;
    var m = month + 12*a - 2;
    var d = (day + y + Math.floor(y/4) - Math.floor(y/100) +
             Math.floor(y/400) + Math.floor((31*m)/12)) % 7;
    return d + 1;
}

function makeArray()    {
    this[0] = makeArray.arguments.length;
    for (i = 0; i<makeArray.arguments.length; i++)
        this[i+1] = makeArray.arguments[i];
}

var daysofmonth   = new makeArray( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var daysofmonthLY = new makeArray( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

function LeapYear(year) {
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}

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;
}

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;

var daysofweek   = new makeArray('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var monthsofyear = new makeArray('January','February','March','April','May','June','July','August','September','October','November','December');

function DayOfWeek(day,month,year) {
    var a = Math.floor((14 - month)/12);
    var y = year - a;
    var m = month + 12*a - 2;
    var d = (day + y + Math.floor(y/4) - Math.floor(y/100) + Math.floor(y/400) + Math.floor((31*m)/12)) % 7;
    return d+1;
}

function Nths(day) {
    if (day == 1 || day == 21 || day == 31) return 'st';
    if (day == 2 || day == 22) return 'nd';
    if (day == 3 || day == 23) return 'rd';
    return 'th';
}

function FullDate(day,month,year) {
    return daysofweek[DayOfWeek(day,month,year)] +' '+ day + Nths(day) +' '+ monthsofyear[month] +' '+ year;
}
//--></SCRIPT>

</HEAD>

<BODY>

<SCRIPT LANGUAGE="JavaScript"><!--
var today = new Date();
var year = y2k(today.getYear());
var month = today.getMonth()+1;
var date = today.getDate();

var day = NthDay(second,tue,month,year);

if (day < date) {
    month++;
    if (month == 13) {
        month = 1;
        year++;
    }
    day = NthDay(second,tue,month,year);
}

document.write(FullDate(day,month,year));
//--></SCRIPT>

</BODY>
</HTML>

©2018 Martin Webb