Date Array Lookup

One thing that you may want to do when setting up On This Day processing is to retrieve a dated entry from an array.

This is particularly easy to do if you specify the month and day at the start of each entry in the array. You don't even need to have entries in the array for every possible date since we can easily arrange to use the next date found following the one we are looking for if the exact date isn't found.

Let's start by creating our array of date related entries. Here's an example with just a few entries.

var datearray = [], x = 0;
datearray[x++] = "0101 New Year";
datearray[x++] = "0214 Valentine's day";
datearray[x++] = "0709 Steve's Birthday";
datearray[x++] = "1020 Site Anniversary";
datearray[x++] = "1206 St Nicholas' Day";

Each entry in the array contains the month and day as a four digit number at the start followed by whatever you may want to associate with that date filling the rest of the entry. My example has the dates in ascending order but it doesn't matter if you have them arranged randomly as you can always sort the array after loading it by adding one more statement.


The next thing that we need is a function that will do the lookup of the array for us. We want to pass a date in the format mmdd and have the entry for that date or the closest date following extracted from the date array with the date information removed from the front of the entry. The following function will do this for us.

function nextDate(mmdd) {
var x = datearray.length;
for (var i = 0; i < x; i++) {
if (datearray[i].substr(0,4) > mmdd)
return datearray[i].substr(4);
return datearray[0].substr(4);

Now all that remains is to call nextDate(mmdd) from where ever it is in our processing where we want to use the entry that we have extracted from our date array, passing it the month and day (as a four digit number mmdd) that we want to use for the lookup.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow