« Sharp Develop Rocks | Main | Ruby on Rails »

January 4, 2006

Date function in Javascript

I have needed to manipulate some dates in a project I am working on. The way I usually did this was to handle incrementing the day, month and year separately. This involved lots of checking leap years and that kind of thing, as it was necessary to check the number of days in each month, watch for year rollovers etc.

There is a MUCH easier way of doing this. I hope. I have done some testing and it all seems to hang together OK so far.

The secret is to convert to milliseconds and then back to a date.

Some code:

function AddDaysToDate( theDate, Days ) { var theDate = new Date( theDate ); var theDate = new Date( theDate.getTime() + (Days * 24 * 60 * 60 * 1000) ); return theDate.getDate() + "-" + (theDate.getMonth() + 1) + "-" + theDate.getFullYear(); }

you will notice that it only uses days. This is to keep it all simple and besides my application only needs days added.

Adding months to the mix

If you wanted to add months then things get a little bit more complex.

We will need to set up an array (may as well make it global) that stores the number of days in every month and then create a function that will update the number of days in February depending on whether the year is a leap year or not.

This is what I came up with:

var MonthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); function CheckLeapYear( theYear ) { if ( theYear % 4 == 0 ) { if ( theYear % 100 == 0 && theYear % 400 == 0 ) MonthDays[1] = 29; if ( theYear % 100 != 0 ) MonthDays[1] = 29; } else { MonthDays[1] = 28; } } function AddDaysAndMonthsToDate( theDate, Days, Months ) { var theDate = new Date( theDate ); var theDate = new Date( theDate.getTime() + (Days * 24 * 60 * 60 * 1000) ); // save the current day var current = theDate.getDate(); // an now set this months day to the first - simplies everything! theDate.setDate( 1 ); // now loop through the months and add each months number of days in milliseconds for ( i = Months ; i > 0 ; i-- ) { CheckLeapYear( theDate.getFullYear() ); // tweaks the number of days in February if necessary var theDate = new Date( theDate.getTime() + (MonthDays[theDate.getMonth()] * 24 * 60 * 60 * 1000) ); } theDate.setDate( current ); return theDate.getDate() + "-" + (theDate.getMonth() + 1) + "-" + theDate.getFullYear(); }

Now we have a function that updates a date by adding the required number of days or months.

It is then trivial to have it add the number of years also:

function AddToDate( theDate, Days, Months, Years ) { var theDate = new Date( theDate ); var theDate = new Date( theDate.getTime() + (Days * 24 * 60 * 60 * 1000) ); // save the current day var current = theDate.getDate(); // an now set this months day to the first - simplies everything! theDate.setDate( 1 ); // now loop through the months and add each months number of days in milliseconds for ( i = Months ; i > 0 ; i-- ) { CheckLeapYear( theDate.getFullYear() ); // tweaks the number of days in February if necessary var theDate = new Date( theDate.getTime() + (MonthDays[theDate.getMonth()] * 24 * 60 * 60 * 1000) ); } theDate.setDate( current ); //now add the years theDate.setYear( theDate.getFullYear() + Years ); return theDate.getDate() + "-" + (theDate.getMonth() + 1) + "-" + theDate.getFullYear(); }

Please let me know about any errors or issue you find.

The scripts should work fine in most browsers but as far as I know the getFullYear() function is a fairly new introduction and may not be supported by old IE browsers on a Mac for instance, but then I did write the functions to be used in ASP so I don't really care! :)

Dont forget to be good little girls and boys now and make all the Javascript external!!

Posted by dottie at January 4, 2006 7:03 PM

Comments

Post a comment




Remember Me?

(you may use HTML tags for style)