计算两个日期之间的差异 - datetimepicker
Count difference between two dates - datetimepicker
- Select
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>
首先,对于这两个日期,您必须将此日期格式从日期和小时转换为毫秒。然后找出毫秒之间的差异。
一分钟 60*1000
一小时 60*60*1000
一天 24*60*60*1000
除以这个特定项并完成工作。
您应该尝试使用名为 moment.js -> http://momentjs.com/docs/#/displaying/difference/ 的 javascript 库
示例:
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 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));
- Select
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>
首先,对于这两个日期,您必须将此日期格式从日期和小时转换为毫秒。然后找出毫秒之间的差异。
一分钟 60*1000 一小时 60*60*1000 一天 24*60*60*1000
除以这个特定项并完成工作。
您应该尝试使用名为 moment.js -> http://momentjs.com/docs/#/displaying/difference/ 的 javascript 库 示例:
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 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));