Javascript DateValidation (compare two dates)

Hi friends,

Below is the code that dose date validation. To be specific, this javascript can be used for comparing two dates (eg., to validate for a case where Start date should be before the End date).

if (Date.parse(fromDate) > Date.parse(toDate)) {
alert("Invalid Date Range!\nStart Date cannot be after End Date!")
return false;

So how to test this code??
Answer: Have two textboxs in your page and ask the user to entire two dates (Start date and End date). If the start date is after the end date, this code would thrown an alert.

You can get the file from here which has code that performs a simple eg.,

Hope this helps you.
Love MJ 🙂

23 Responses

  1. u’r sample code will not working properly,test it once

  2. I gave 12-08-2010 in the start date and 12-08-2009 in the end date and I got a alert saying ‘Invalid date’.

    So, It working buddy ;).

    Can you please tell me the reaon for your comment? I’ll try to answer your question.


  3. Hi,

    When i put your code in my javascript function Date.parse method gives me “NaN”.
    Could you tell me just other method which has javascript support ?


  4. Hello CodVenture, Please give me a explanation on what exactly you are looking for so that I may try to solve your issue.

    This code accepts only numerics. Probabily you would have used ‘mar’ for march which should be 03

    If you still find this as an issue please give me your exact scenario so that i can help you to modify the code accordingly


  5. Try this ….
    var startDateValue = new Date(startDateVal);
    var startDateValuecmp = startDateValue.getTime();
    var endDateValue = new Date(endDateVal);
    var endDateValuecmp = endDateValue.getTime();
    if( startDateValuecmp > endDateValuecmp ){
    alert(“End Date cannot be less than Start Date”);

    Regards Nitil

  6. not working

  7. The code from Freddie is working fine on IE 7 and IE 8. But it does not work on Firefox 3.6.4. Can anyone explain why it is going on?

  8. i want that future date should not select when we select the future date and print the error message that sorry you can not select the future date. Please tell me that how can i solve this problem.

    • Anonymous:
      Solution for u r problem
      $.validator.addMethod(“DateFormat”, function(value,element) {

      var validDateFormat= value.match(/^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/);

      if(Date.parse(value)<= Date.parse(todayDate())){
      return true;

      return false;

  9. Your validation is not working properly.
    Suppose we have 03-01-2012 as start date and 02-02-2012 as end date… the function will alert ‘invalid range’ as the end date will be less when passing through the Date.parse function.02-02-2012 is in the future. any solution man??

    thank you

  10. Any source code blocks look like this
    t1=”10/10/2006″ ;


    //Total time for one day
    var one_day=1000*60*60*24;
    //Here we need to split the inputed dates to convert them into standard format
    for furter execution
    var x=t1.split(“/”);
    var y=t2.split(“/”);
    //date format(Fullyear,month,date)

    var date1=new Date(x[2],(x[1]-1),x[0]);

    var date2=new Date(y[2],(y[1]-1),y[0])
    var month1=x[1]-1;
    var month2=y[1]-1;

    //Calculate difference between the two dates, and convert to days

    //_Diff gives the diffrence between the two dates.

  11. thank you so much that code very helpfull to me once again thank you

  12. can give the complete coding

  13. this script not working for feburary month it allows 30,31 date as well allow date 31 for even months like april ,

  14. The operator > is undefined for the argument type(s) Date, Date is displayed

  15. its work fine form me tq…. simple code … good

  16. function nofuturdate(testdate)

    var todaydate=new Date();

    var userenterdate=testdate.value;
    var todayday=todaydate.getDate();
    var curmonth=todaydate.getMonth()+1;
    var curyer=todaydate.getFullYear();

    var todcompletedate=todayday+”/”+ curmonth+”/”+curyer;

    var check=todcompletedate.localeCompare(userenterdate);
    alert(“Past Date”);

    alert(“Future Date”);


    //// i want a function that accept only DDMMYYYY format and not allowed future date to entered , Please anyone help me

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: