Select start date and hour
Select end date and hour
我想在Show Days difference here
和Show Hours difference here
请参阅以下代码段以供参考
$(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>
答案 0 :(得分:1)
你应该尝试使用名为moment.js的javascript库 - &gt; http://momentjs.com/docs/#/displaying/difference/ 例如:
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1
答案 1 :(得分:1)
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));
答案 2 :(得分:0)
首先,您必须将这些日期格式从日期和小时转换为两个日期的毫秒。然后找出毫秒之间的差异。
一分钟60 * 1000 一小时60 * 60 * 1000 一天24 * 60 * 60 * 1000
除以此特定并完成工作。