Compare entered date with current date

Discussion in 'JavaScript' started by MSK7, Jan 2, 2010.

  1. #1
    Hello all,

    I am entering date from a calender in a textbox named "date" in the formate of (yyyy-mm-dd) like (2010-01-02).

    I want to compare the entered date with todays current date ,
    so that past date can not be submitted in the textbox.

    i am using the code below to get todays current date to compare with text box value :-
    
    var currentTime = new Date();
    var Cmonth = currentTime.getMonth() + 1 ;
    var Cday = currentTime.getDate();
    var Cyear = currentTime.getFullYear();
    var Today = (Cyear + "-" + Cmonth + "-" + Cday);
    alert(Today);
    
    Code (markup):
    But it gives todays date in the form of (2009-1-2)
    rather than (2009-01-02).

    please suggest me how this date can be achieved like (2009-01-02).

    Please also suggest how the entered date can be compared with the achieved Todays current date ,
    so that past date cannot be submitted.


    Thanks & Regards.
     
    MSK7, Jan 2, 2010 IP
  2. Randombase

    Randombase Peon

    Messages:
    224
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hello,

    To achieve the zeroes in front of the code you can simply check if the day/month is smaller than ten, if yes, add a 0. Example:
    var currentTime = new Date();
    var Cmonth = currentTime.getMonth() + 1 ;
    var Cday = currentTime.getDate();
    var Cyear = currentTime.getFullYear();
    Cday = (Cday < 10) ? '0' + Cday : Cday;
    Cmonth = (Cmonth < 10) ? '0' + Cmonth : Cmonth;
    var Today = (Cyear + "-" + Cmonth + "-" + Cday);
    alert(Today);
    Code (markup):
    To compare dates I suggest you check the w3 schools page on it: http://www.w3schools.com/jS/js_obj_date.asp
     
    Randombase, Jan 2, 2010 IP