1. Computing

JavaScript Dates

The Easy Way to Work With Dates and Times in JavaScript

From , former About.com Guide

The JavaScript date object is the best way of handling all of the date and time processing in your JavaScript code. By using the JavaScript date object you don't need to worry about where adding or subtracting hours results in changing the day, you don't need to worry about how many days there are in a given month and whether it is a leap year, and adjusting the date/time for a specific timezone becomes a trivial task. Everything that you could possibly want to do with dates or times in JavaScript can be handled with a minimum of fuss by using the JavaScript date object.

The JavaScript date object comes with a number of built in methods to allow you to set and retrieve the different parts of the date and time very easily. These methods are somewhat limiting though as they only work at the most primitive of levels (while successfully hiding the internal processing that makes using the JavaScript date object essential). To make our use of JavaScript dates in our code easier we can add our own methods to the JavaScript date object which build on these primitive methods to provide more useful processes. There are three types of things that we need to be able to do with dates so we can add methods to perform each of these different types of tasks.

  • adding or subtracting periods
  • comparing dates
  • displaying dates

Let's consider what methods that we can add to the JavaScript date object to make each of these three types of task easier. With adding and subtracting periods we really only need to worry about adding since if we add a negative period we get the same result as if we subtract a positive period. Another aspect to adding and subtracting periods is that the code is very similar regardless of the size of the periods we are dealing with so just by substituting the appropriate methods we can create a series of new methods to deal with each different period size. So it doesn't really matter whether we are Adding Days to a Date or years or seconds the code we need for our new method is almost the same and does away with our needing to worry about if changing the daye by a number of days moves us into a different month or even a different year as the JavaScript date object takes care of that for us automatically.

There is a bit more variety when it comes to comparing dates. Obtaining the Day Difference and Business Day Difference between two dates require two slightly different processes while determining someone's Age Last Birthday requires a completely different calculation in order to be accurate. We can of course easily add all of these methods to all our JavaScript date objects in one go so as to be able to easily determine these values whenever we need them without having to write new code each time we need it.

It is in the area of formatting dates where there is the greatest opportunity for extending the JavaScript date object as there are many date/time related values that cannot be directly retrieved from a date using the primitive methods supplied but which can be extracted using a few simple calculations. By adding methods to our JavaScript date objects that perform these calculations we can easily obtain dates and times in whatever format we require in a consistent fashion. By adding a few simple methods we can obtain simple information about a date such as the Day of the Year, Month and Day names (either in English or whatever language you prefer), what Week of the Year it is (remembering that JavaScript starts the week on Sundays), and even whether the local computer is in a location that uses Daylight Saving Time provided that it isn't on DST all year round. We can also obtain more complex information relating to dates such as determining the current Julian Day or getting the time as Swatch Beats.

Not everyone considers the week to start on the same day so we can also Change When the Week Starts and having done that we can determine the ISO standard week and year (where the week starts on Monday and the week in which the year changes belongs to the year that has the most days).

Of course you will often want to extract several of these date/time components from your JavaScript date all at the same time. Some scripting languages (such as PHP) supply a simple format method that allows you to extract a date and time in whatever fancy format you require and while the JavaScript date object doesn't provide such a method we can easily add our own format method that does everything the PHP one does (apart from a couple of timezone values that JavaScript can't access) and more.

Overall the JavaScript date object is a powerful way of performing all your date/time processing in JavaScript particularly after you add all the methods that will handle all of the things you are ever likely to want to do with dates and times for you with a simple method call.

If you want to grab all the extra JavaScript date methods mentioned in one go then Steve's Ultimate JavaScript Date Library is a zip file containing all of the extra methods mentioned above and more.

©2013 About.com. All rights reserved.