1. Computing & Technology

Date Methods

Join the Discussion

Questions? Comments?

JavaScript allows you to extend the functionality of any of the built in objects simply by defining the additional methods that you require. One such built in object that we can add to like this is the date object. By adding extra methods to the built in date object we make it extremely easy to obtain whatever information that we require about any of the dates that our script is using.

What sorts of things can we add to our date processing? Well the following code will add the necessary methods to all your dates to allow you to add a nu8mber of days to a date, to find out which week of the year a date is in, to determine what day of the year the date is, to get the julian day, as well as to retrieve the name associated with the month and the day of the week. (You may need to change these last two slightly if your target language isn't English).

With these added to the top of the JavaScripts on your page you can do a simple call to achieve any of these for any date object you have defined in your code.

Simply past the above code into your page and then call the various methods as shown in the comments at the top of the script.

©2012 About.com. All rights reserved.

A part of The New York Times Company.