Go4Expert

Go4Expert (http://www.go4expert.com/)
-   JavaScript and AJAX (http://www.go4expert.com/articles/javascript-tutorials/)
-   -   Date Class in JavaScript (http://www.go4expert.com/articles/date-class-javascript-t520/)

pradeep 28Nov2005 18:10

Date Class in JavaScript
 

Introduction



The Date class in JavaScript.This class not only makes the manipulation of dates easier (regardless of which date format you prefer) but it also handles time as well.

Any time that you have a javascript that is required to do any sort of processing based on either the current date and time or which involves the comparison of two dates or times then using the Date class will make that processing much simpler.

Defining Dates



To be able to use the Date class we need to define objects of that type. To do this we use a command very similar to that which we used to define arrays.

The way to define an object as a date is like this:

Code: JavaScript

var myDate = new Date;

The date object will automatically be assigned the current date and time as its initial value. We can easily override the component parts of the date by using the various methods available for manipulating dates.

For example, to set our date object to 15th April 2006 you can specify the following immediately after the above declaration:
Code: JavaScript

myDate.setDate(15);
  myDate.setMonth(3); // January = 0
  myDate.setFullYear(2006);

Comparing Dates



One of the most useful areas where using the date class is useful is where you need to either compare two dates or to change a date or time by a specified amount. For example let's say that we want to compare today's date with the 15th April 2006. Is today before or after that date? To do this we need to add the following code to what we have already defined above:

Code: JavaScript

var today = new Date;
  if (myDate < today)
    alert('today is before 16th April 2006');
  else
    alert('today is after 15th April 2006');

Note that since the time of setting the today object is after that of setting the myDate object we know that myDate will still be less than today on that date itself (as we have not changed the value held in any of the time portions of either object from that originally set).


Changing Dates



As well as comparing dates we can also easily manipulate dates. For example say that we need to work with the date exactly one week in the future All we need to do with a date object is to add 7 to the days portion of the date like this:

Code: JavaScript

myDate.setDate(myDate.getDate()+7);

If adding seven days to the date shifts the date into the following month or even the following year then the changes to the month and year portions of the date are handled automatically by the date class itself internally, we don't need to code anything ourselves to handle this. This of course if the biggest benefit of having predefined classes such as this one, much of the complicated processing is handled for you.


All times are GMT +5.5. The time now is 17:35.