datepicker calculations
-
14-06-2021 - |
Вопрос
I have the following code to ensure that if the user selects the same check in date as check out date for the conference, then the "total_days" = 1 if the difference in dates is > 0 - then it is supposed to update one more day for each one. ie "conference date in = 13.06.12" "conference date out = 13.06.12" - total days = 1 - this is correct. however, if "conference date in = 13.06.12", and "conference date out = 14.06.12", then "total_days" needs to be 2, instead of 1. what am I doing wrong? I have tried the following:
<td>Conference Date In</td>
<td><input type="text" name="conference_date_in" id="conference_date_in" class="datepicker" /></td>
<td> </td>
<td>Conference Date Out</td>
<td><input type="text" name="conference_date_out" id="conference_date_out" class="datepicker" /></td>
</tr>
<td>Total Days</td>
<td><input type="text" name="total_days" id="total_days" /></td>
</tr>
<script type="text/javascript">
$(function() {
$(".datepicker").datepicker({ minDate: -0, maxDate: "+100M +10D",dateFormat: 'dd-mm-yy'})
({
changeMonth: true,
changeYear: true,
});
});
var enquiry_date = $.datepicker.formatDate('dd-mm-yy', new Date());
document.getElementById('enquiry_date').value = enquiry_date;
var calcDate = function() {
var start = $('#conference_date_in').datepicker('getDate');
var end = $('#conference_date_out').datepicker('getDate');
var days = (end - start) / 1000 / 60 / 60 / 24;
if(days==0) {days=1
}
if( days >= 0 ) {
document.getElementById('total_days').value = days;
}
}
$('#conference_date_out').change(calcDate);
$('#conference_date_in').change(calcDate);
Решение
Posting this ass answer to close the issue.
Can't you just add 1 to the result of (end - start) / 1000 / 60 / 60 / 24;
?
You have to if's not if..else so this should resolve your issue.