Hitendra Patil
Hitendra Patil

Reputation: 1

couldn't find the difference between two date in another textbox in javascript

my date format is dd/mm/yyyy and calculate this format

here a script

<script type="text/javascript">
  function GetDays() {

    var dropdt = new Date(document.getElementById("sdate").value);
    var pickdt = new Date(document.getElementById("edate").value);
    var difference = edate - sdate;
    return Math.round(difference / 1000 * 3600 * 24);
  }

  function cal() {

    document.getElementById("numdays2").value = GetDays();

  }
</script>

Upvotes: 0

Views: 41

Answers (2)

Damodhar
Damodhar

Reputation: 1317

you need to get time from date and then do the calculation

var date1 = new Date("06/30/2019"); 
var date2 = new Date("07/30/2019"); 

// To calculate the time difference of two dates
var Difference_In_Time = date2.getTime() - date1.getTime(); 

// To calculate the no. of days between two dates
var Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24); 

//To display the final no. of days (result)
console.log("Total number of days between dates  <br>"
               + date1 + "<br> and <br>" 
               + date2 + " is: <br> " 
               + Difference_In_Days); 

Upvotes: 1

Dmitry Reutov
Dmitry Reutov

Reputation: 3032

at the first place identifiers are wrong at the second place use getTime function to get milliseconds at the third place add brackets to evaluation expression to get difference in days

var difference = dropdt.getTime() - pickdt.getTime();
return Math.round(difference / (1000 * 3600 * 24));

Upvotes: 0

Related Questions