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

Easter

The 3rd Saturday in November

Born on the 4th of July

The Chinese New Year

What sign are you?

Monday's child is full of grace

Extending "Born of the 4th of July"

You are here: irt.org | Articles | JavaScript | Date and Time | Extending "Born of the 4th of July" [ previous next ]

Published on: Friday 26th December 1997 By: Martin Webb

Introduction

This article describes how to merge all the calendar events and holidays calculated in the three different articles Born of the 4th of July, Easter and The 3rd Saturday in November into one object array.

We'll also show how to sort the array in JavaScript 1.1 browsers, and how to simulate an array sort using linked lists.

Creating the Database

In the previous article Born of the 4th of July we created the myEvent[] array of Event objects using the following script:

function Event(day,month,what) {
    this.day = day;
    this.month = month;
    this.what = what;
}

function setEvent(day,month,what) {
    myEvent[eventIndex++] = new Event(day,month,what);
}

var eventIndex = 0;
var myEvent = new Array();

So for example, we can create an Event object for New Years Day with:

setEvent( 1,jan,"New Year's Day");

In the previous article Easter we calculated the date of Easter Sunday and the other religious calendar events dependent on Easter using the Easter(), getDD(), getMM(), addDays() and getMMDD() functions.

So for example we can create an Event object for Easter Sunday with:

var easter = Easter(year);
var easterDay = getDD(easter);
var easterMonth = getMM(easter);

setEvent(easterDay,easterMonth,"Easter Sunday");

We can create an Event object for Ash Wednesday with:

var when = addDays(easterDay,easterMonth,year,-46);
setEvent(getDD(getMMDD(when)),getMM(getMMDD(when)),"Ash Wednesday");

In the previous article The 3rd Saturday in November we calculated the dates of the various variable calendar events using the NthDay() function.

So for example we can create an Event object for Mother's Day with:

setEvent(NthDay(second,sun,may,year),may,"Mother's Day");

The FullDate() function used in the above scripts is described in the previous article Blind Date

Displaying the Calendar Events

To show any calender events on a particular date we can use the following showEvent() function:

function showEvent(day,month,year) {
    var output = '';
    for(var i=0; i < eventIndex; i++) {
        if ((day == myEvent[i].day) && (month == myEvent[i].month))
            output += FullDate(myEvent[i].day,myEvent[i].month,year) + ' ' + myEvent[i].what+'<BR>';
    }
    return output;
}

To show all the calendar events in the myEvent[] array we can use the following ShowAllEvents() function:

function showAllEvents(year) {
    var output = '';
    for(var i=0; i < eventIndex; i++) {
        output += FullDate(myEvent[i].day,myEvent[i].month,year) + ' ' + myEvent[i].what+'<BR>';
    }
    return output;
}

Which we can invoke with:

document.write(showAllEvents(year));

Sorting the Events

It soon becomes apparent that no matter in what order you create the Event objects, because the dates of events alter each year, then the myEvent[] will not hold the data sorted into date order.

We'll use the myObjectBubbleSort() function described in the previous article Arrays, Object Arrays and Sorting to sort the events into date order.

To enable the Event objects to be sorted, the Event() function must be modified to include a new value property:

function Event(day,month,what)    {
    this.value = '' + padout(month) + '/' + padout(day);
    this.day = day;
    this.month = month;
    this.what = what;
}

We have not included the year in the value property, as it is assumed that the year will be the same for all the Event objects. If it isn't then it only requires an extra parameter to the Event() function.

When sorting the myEvent[] object array using the myObjectBubbleSort() function, all we need to do is pass the name of the object array and the size of the array:

myObjectBubbleSort(myEvent,eventIndex);

As the myObjectBubbleSort() function uses the objects value property to sort the objects in the object array, then the Event objects will be sorted on month and day.

Working Example

Try the frame version which shows all the religious dates for the current year in date order.

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

Easter

The 3rd Saturday in November

Born on the 4th of July

The Chinese New Year

What sign are you?

Monday's child is full of grace

©2018 Martin Webb