Reputation: 1
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
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
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