我希望将时间添加到日期中,并希望通过减去EndDate和StartDate来获得时间。以下是我的尝试。请帮助我
var selectedStartDate;
var selectedEndDate;
$("#StartDate").change(function () {
selectedStartDate = $('#StartDate').datepicker({ dateFormat: 'dd/MM/yyyy' }).val();
alert(selectedStartDate);
});
$("#EndDate").change(function () {
debugger;
selectedEndDate = $('#EndDate').datepicker({ dateFormat: 'dd/MM/yyyy' }).val();
alert(selectedEndDate);
selectedEndDate = selectedEndDate + ' ' + '12:00:00 AM';
selectedStartDate = selectedStartDate + ' ' + '10:00:00 AM';
var timeDiff = Math.abs(selectedEndDate- selectedStartDate);
alert(timeDiff);
});
答案 0 :(得分:0)
你在这里。我重构了代码,我想我们不需要时间,否则应该使用datetimepicker而不是datepicker:
var selectedStartDate;
var selectedEndDate;
$("#StartDate").datepicker({
dateFormat: 'dd/mm/yy',
onSelect: onSelect
});
$("#EndDate").datepicker({
dateFormat: 'dd/mm/yy',
onSelect: onSelect
});
function onSelect(dateText, inst) {
selectedStartDate = $("#StartDate").datepicker("getDate");
selectedEndDate = $("#EndDate").datepicker("getDate");
if (selectedStartDate != null && selectedEndDate != null) {
var timeDiff = Math.abs(selectedEndDate.getTime() - selectedStartDate.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
alert(diffDays);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" />Start:
<input type="text" id="StartDate" />End:
<input type="text" id="EndDate" />
希望得到这个帮助。