On This Day

Do you have some particular processing that you want to perform based on which day of the year that it is? Perhaps you have something different that you want to display depending on the day of the year?

While server side processing is the better option for determining which day it is, not everyone has web hosting that provides the access to perform that process on the server. If that is your situation then you may be looking for JavaScript to provide you with an alternative way to perform the day of the year selection.

Whatever processing you intend to do based on the date, the first thing that you need to do is to obtain the date. The following code retrieves the month and day and returns a four digit number in the format mmdd.

function getmmdd() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
return (mm > 9 ? '' + mm : '0' + mm) + (dd > 9 ? dd : '0' + dd);
}

We can then use that value to perform whatever processing that we want to use based on the particular date.

The simplest code is where we want a link to go to a different page depending on which day of the year it is. For example we might use:

<a href="notip.htm"
onclick="location.href='tip'+getmmdd()+'.htm';;return false;"
>Tip of the Day</a>

This link would transfer those people with JavaScript enabled to date specific web page (tipmmdd.htm) while those without JavaScript would end up on a particular page (notip.htm) instead.

Of course there are lots of other ways that you can use the value returned from this function to perform date specific processing within any JavaScript that we have on the current page.

 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate