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

Related items

And now...The Weekly Update Script

The 24 Hour World

Today's The Day

Extending "Born of the 4th of July"

Easter

The 3rd Saturday in November

Born on the 4th of July

The Chinese New Year

What sign are you?

32 years 8 months 24 days

Monday's child is full of grace

You are here: irt.org | Articles | JavaScript | Date and Time | Monday's child is full of grace [ previous next ]

Published on: Saturday 18th October 1997 By: Martin Webb

Introduction

This article will demonstrate how to calculate the day of the week of any date.

Using this information we will retrieve the relevant line from "Monday's Child":

Monday's child is fair of face,
Tuesday's child is full of grace,
Wednesday's child is full of woe,
Thursday's child has far to go,
Friday's child is loving and giving,
Saturday's child works hard for a living,
And the child that is born on the Sabbath day
Is bonny and blithe and good and gay.

It will also describe how to display the full information for the date given, e.g. Tuesday 28th October 1997.

Days of the Week

The following piece of code creates two arrays daysofweek[] and child[] using the makeArray() function as described in the previous article Blind Date.

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

var daysofweek = new makeArray('Sunday',
                               'Monday',
                               'Tuesday',
                               'Wednesday',
                               'Thursday',
                               'Friday',
                               'Saturday');

var child      = new makeArray('is bonny and blithe and good and gay',
                               'is full of grace',
                               'is fair of face',
                               'is full of woe',
                               'has far to go',
                               'is loving and giving',
                               'works hard for a living');

Calculating the day of the week

The following was taken from The Calendar FAQ at http://www.tondering.dk/claus/calendar.html

To calculate the day on which a particular date falls, the following algorithm may be used (the divisions are integer divisions, in which the remainders are discarded):

a = (14 - month) / 12
y = year - a
m = month + 12*a - 2
d = (day + y + y/4 - y/100 + y/400 + (31*m/12) % 7

The value of d is 0 for a Sunday, 1 for a Monday, 2 for a Tuesday, etc.

This can be converted into the following simple script, where Math.floor converts floating point numbers to integers:

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

Converting it to a named day

To test the function out, we'll find the day of the week that I was born on, by using the daysofweek[] array to display the day name:

document.write(daysofweek[DayOfWeek(4,1,1965) + 1]);

Which when run produces the following:

However, rather than remember to have to add 1 to the result each time, it would be better to recode the DayOfWeek() function as:

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

Now we would use the following to find out the day of the week of Christmas Day 1900:

document.write(daysofweek[DayOfWeek(25,12,1900)]);

Which when run produces the following:

Monday's Child

Now that we have the integer that refers to the day of the week, we can used the child[] array created earlier to show the relevant line from "Monday's Child" using the following Child() function:

function Child(dayofweek) {
    return daysofweek[dayofweek] + '\'s child ' + child[dayofweek];
}

document.write(Child(DayOfWeek(25,12,1900)));

Which when run produces:

The whole date and nothing but the date

With the addition of the monthsofyear[] array and the following two functions Nths() and FullDate() we can show the full information for a given date, e.g. Tuesday 28th October 1997.

var monthsofyear = new makeArray('January',
                                 'February',
                                 'March',
                                 'April',
                                 'May',
                                 'June',
                                 'July',
                                 'August',
                                 'September',
                                 'October',
                                 'November',
                                 'December');

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

For example:

document.write(FullDate(1,1,2000));

Which is the first day of the year 2000, returns:

Working Example

Why not try it out yourself. Do you actually know the day you were born on? Try the frame version.

Source Code

You can view the source code of the four components:

Related items

And now...The Weekly Update Script

The 24 Hour World

Today's The Day

Extending "Born of the 4th of July"

Easter

The 3rd Saturday in November

Born on the 4th of July

The Chinese New Year

What sign are you?

32 years 8 months 24 days

©2018 Martin Webb