【发布时间】:2016-11-09 22:19:28
【问题描述】:
我正在尝试从我拥有的两个自定义 DateTimePickers 中获取总天数。
我试过这个功能:
function dateDifference() {
if($("#fradato").val()!='' && $("#fradato").val()!='') {
var diff = ($("#fradato").datepicker("getDate") - $("#tildato").datepicker("getDate")) / 1000 / 60 / 60 / 24;
$("#antalldager").val(diff)
我遇到的问题是,无论我在哪里尝试调用此函数,DateTimePicker 都会停止工作。那么我应该在哪里/如何调用这个函数,或者有更好的方法吗?
我想在这个文本框内显示总天数:
<div class="form-group">
<label class="control-label col-sm-1 col-sm-offset-3" for="antalldager">Antall dager:</label>
<div class="col-sm-2">
<input type="text" class="form-control" id="antalldager" disabled>
这是我的html:
<script>valgavdato() </script>
<div id="visdager" style="display: none;">
<label class="control-label col-sm-2 col-sm-offset-3" for="fradato">Book fra:</label>
<div class="container">
<div class="row">
<div class='col-sm-2'>
<div class="form-group">
<div class='input-group date'>
<input type="text" id="fradato" class="form-control"/>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar" ></span>
</span>
</div>
</div>
</div>
</div>
</div>
<div id="vistildato" style="display: none;">
<label class="control-label col-sm-2 col-sm-offset-3" for="tildato">Book til:</label>
<div class="container">
<div class="row">
<div class='col-sm-2'>
<div class="form-group">
<div class='input-group date'>
<input type="text" id="tildato" class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
</div>
JS:
$( function valgavdato() {
var dateFormat = "mm/dd/yy",
to_MaxDate = 13; // From date + this = to maxDate
from = $( "#fradato" )
.datepicker({
minDate: '0+',
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1
})
.on( "change", function() {
var PickedDate = getDate( this );
// See that date is in UTC format.
console.log( "From DatePicker: "+JSON.stringify(PickedDate) );
// Process the picked date.
var tempDay = PickedDate.getDate() + to_MaxDate; // Add a number of days to the picked date.
var tempMonth = PickedDate.getMonth() + 1; // Because months are zero based.
var tempYear = PickedDate.getYear() + 1900; // Because years are 1900 based
console.log( "Temp date: "+ tempYear+"/"+tempMonth+"/"+tempDay +" --- It may look impossible... But Date() handles it.");
// Create a date object in a UTC format.
var newMaxDate = new Date(Date.UTC(tempYear, tempMonth-1, tempDay, 0, 0, 0));
console.log( "New max date: : "+ JSON.stringify(newMaxDate) );
// Set the minDate ans maxDate options.
to.datepicker( "option", {"minDate": PickedDate, "maxDate": newMaxDate});
}),
to = $( "#tildato" ).datepicker({
maxDate: '14+',
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1
})
.on( "change", function() {
from.datepicker( "option", "maxDate", getDate( this ) );
});
function getDate( element ) {
var date;
try {
date = $.datepicker.parseDate( dateFormat, element.value );
} catch( error ) {
date = null;
}
return date;
}
} );
function dateDifference() {
if($("#fradato").val()!='' && $("#fradato").val()!='') {
var diff = ($("#fradato").datepicker("getDate") - $("#tildato").datepicker("getDate")) / 1000 / 60 / 60 / 24;
$("#antalldager").val(diff)
}
}
【问题讨论】:
-
日期计算总是非常棘手(太多的例外、不同的月份长度、时区、冬季/夏季时间)。可能值得看看一些实现,例如momentjs.com/docs/#/displaying/difference
-
嗯,好的,我会检查一下。所以我可以使用 Moment 函数,并以某种方式获取时刻内的日期来计算差异?
-
是的,没错
标签: jquery jquery-ui datetimepicker