Date Class in JavaScript

Discussion in 'JavaScript and AJAX' started by pradeep, Nov 28, 2005.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in

    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:
    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:
      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:
    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:
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice