1. Home
  2. Computing & Technology
  3. JavaScript

On This Day

Join the Discussion

Questions? Comments?

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 Javascripts that we have on the current page.

Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.