|
Monday's child is full of grace
You are here: irt.org | Articles | JavaScript | Date and Time | Monday's child is full of grace
Published on: Saturday 18th October 1997 By: Martin Webb
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.
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');
|
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;
}
|
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:
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:
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:
Why not try it out yourself. Do you actually know the day you were
born on? Try the frame version.
You can view the source code of the four components:
Feedback on 'Monday's child is full of grace'
- Monday's child is full of grace - Renee October 23, 2002 at 00:49:55:
- Monday's child is full of grace - Maurna October 11, 2002 at 08:10:31:
- Monday's child is full of grace - Megan October 01, 2002 at 17:17:49:
- Monday's child is full of grace - Cortnee September 04, 2002 at 10:33:08:
- Monday's child is full of grace - abe August 14, 2002 at 17:58:15:
- Monday's child is full of grace - diane August 02, 2002 at 09:33:16:
- Monday's child is full of grace - ANN FALLS June 25, 2002 at 10:40:03:
- Monday's child is full of grace - Ralph June 10, 2002 at 14:10:48:
- Monday's child is full of grace - kim June 06, 2002 at 19:35:16:
- Monday's child is full of grace - kim May 19, 2002 at 14:24:31:
- Monday's child is full of grace - Wayne April 24, 2002 at 23:19:07:
- Monday's child is full of grace - Lee Monroe April 13, 2002 at 23:28:20:
- Monday's child is full of grace - cryger101 March 11, 2002 at 23:19:14:
- Monday's child is full of grace - Kristy Benac February 09, 2002 at 09:34:04:
- Monday's child is full of grace - Tom October 24, 2001 at 16:24:29:
- Monday's child is full of grace - dave simmons 04:17:53 3/05/01
- Monday's child is full of grace - Ross Holder 18:00:23 11/29/00
- Monday's child is full of grace - Sandra 11:41:19 7/26/00
- Monday's child is full of grace - Ed Hodges 22:24:31 11/12/99
- Monday's child is full of grace - pj burrows 17:12:22 2/27/99
View the profile on Martin Webb and the list of other Articles by Martin Webb.
|
|