- 选择
选择开始日期和时间
- 然后 >
选择结束日期和时间
我想在此处显示天数差异
和此处显示时间差异
中显示这两个日期和时间之间的差异
请参阅以下代码片段以供引用
$(function() {
// defualt script for datetimepicket
$('.startDateTime').datetimepicker({
//uncomment following code to enable debug mode
//debug: true
});
$('.endDateTime').datetimepicker({
//uncomment following code to enable debug mode
//debug: true,
useCurrent: false
});
$(".startDateTime").on("dp.change", function(e) {
$('.endDateTime').data("DateTimePicker").minDate(e.date);
});
$(".endDateTime").on("dp.change", function(e) {
$('.startDateTime').data("DateTimePicker").maxDate(e.date);
});
});
<script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<link href="http://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/build/css/bootstrap-datetimepicker.css" rel="stylesheet" />
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/src/js/bootstrap-datetimepicker.js"></script>
<div class="container">
<div class="row date-pick">
<div class="col-md-12">
<div class="well date-picker-box">
<div class="col-md-6">
<input type="text" class="form-control startDateTime" placeholder="Select start date and hour">
</div>
<div class="col-md-6">
<input type="text" class="form-control endDateTime" placeholder="Select End date and hour">
</div>
</div>
</div>
</div>
<input type="text" class="form-control days-here" placeholder="Show Days difference here">
<input type="text" class="form-control time-here" placeholder="Show Hours difference here">
</div>
最佳答案
JS
function CalcDiff() {
var a = $('.startDateTime').data("DateTimePicker").date();
var b = $('.endDateTime').data("DateTimePicker").date();
var timeDiff = 0
if (b) {
timeDiff = (b - a) / 1000;
}
var DateDiff = Math.floor(timeDiff / (60 * 60 * 24));
var BalSecs = timeDiff - (DateDiff * (60 * 60 * 24));
$('.days-here').val(DateDiff)
$('.time-here').val(Math.ceil(BalSecs / (60 * 60)))
}
在dp.change
上调用上述函数
$(".endDateTime").on("dp.change", function (e) {
$('.startDateTime').data("DateTimePicker").maxDate(e.date);
CalcDiff()
});
计算天数后,您已经计算了小时数,因此逻辑
var DateDiff = Math.floor(timeDiff / (60 * 60 * 24));
var BalSecs = timeDiff - (DateDiff * (60 * 60 * 24));
关于jquery - 计算两个日期之间的差异 - datetimepicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32306760/