How to compare dates in javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghjk
    Contributor
    • Jan 2008
    • 250

    How to compare dates in javascript

    I want to compare date in java script for validation. This is my code.
    Code:
            var currentDate = new Date()
     	var day = currentDate.getDate()
      	var month = currentDate.getMonth()
      	var year = currentDate.getFullYear()
    	var today = (year + "-" + month + "-" + day);
    ...
    else if (ED > today){
    	err= 'End Date cannot be grater than the current date!';
    There are two text boxes to enter date. one is start date and the other one is end date. When user enter end date which is grater than start date I want to give error message. But I'll get error message when end date is current date. Where I goes wrong?

    Ex: Start Date :2009-11-30
    End Date: 2009-12-01
    if the current date is 2009-12-02. I have got error message.
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    Code:
    var newDate = new Date(y,m,d);
    var today = new Date();
    dif = newDate-today;//difference in milliseconds
    dif = Math.ceil(dif/1000/60/60/24);//difference in days
    f.difD.value = dif + ' days';//assign the difference
    This code will give you the exact day difference between two dates... The y, m, d in the first line of the code are the user entered date, month and year.

    Thanks and Regards
    Ramanan Kalirajan

    Comment

    Working...