JQuery 计算 2 个日期文本框中的日差
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2609513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery Calculate Day Difference in 2 date textboxes
提问by Aneef
I have 2 asp.net texboxes with calendar extender. I want to find out the number of days between both dates when one of the date control is changed. how can I achieve this using jquery or javascript?
我有 2 个带有日历扩展器的 asp.net texbox。我想找出更改日期控件之一时两个日期之间的天数。我如何使用 jquery 或 javascript 实现这一目标?
回答by ma?ek
This should do the trick
这应该可以解决问题
var start = $('#start_date').val();
var end = $('#end_date').val();
// end - start returns difference in milliseconds
var diff = new Date(end - start);
// get days
var days = diff/1000/60/60/24;
Example
例子
var start = new Date("2010-04-01"),
end = new Date(),
diff = new Date(end - start),
days = diff/1000/60/60/24;
days; //=> 8.525845775462964
回答by Aravindh Gopi
1) Html
1)HTML
<input type="text" id="firstDate" name="firstDate"/>
<input type="text" id="secondDate" name="secondDate"/>
2) Jquery
2) 查询
$("#firstDate").datepicker({
});
$("#secondDate").datepicker({
onSelect: function () {
myfunc();
}
});
function myfunc(){
var start= $("#firstDate").datepicker("getDate");
var end= $("#secondDate").datepicker("getDate");
days = (end- start) / (1000 * 60 * 60 * 24);
alert(Math.round(days));
}
Jsfiddle working example here
Jsfiddle 工作示例在这里
回答by Said Erraoudy
Hi, This is my example of calculating the difference between two dates
嗨,这是我计算两个日期之间差异的示例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://code.jquery.com/jquery.min.js"></script>
<title>JS Bin</title>
</head>
<body>
<br>
<input class='fromdate' type="date" />
<input class='todate' type="date" />
<div class='calculated' /><br>
<div class='minim' />
<input class='todate' type="submit" onclick='showDays()' />
</body>
</html>
This is the function that calculates the difference :
这是计算差异的函数:
function showDays(){
var start = $('.fromdate').val();
var end = $('.todate').val();
var startDay = new Date(start);
var endDay = new Date(end);
var millisecondsPerDay = 1000 * 60 * 60 * 24;
var millisBetween = endDay.getTime() - startDay.getTime();
var days = millisBetween / millisecondsPerDay;
// Round down.
alert( Math.floor(days));
}
I hope I have helped you
我希望我帮助了你
回答by Ravi Shankar
Number of days calculation between two dates.
计算两个日期之间的天数。
$(document).ready(function () {
$('.submit').on('click', function () {
var startDate = $('.start-date').val();
var endDate = $('.end-date').val();
var start = new Date(startDate);
var end = new Date(endDate);
var diffDate = (end - start) / (1000 * 60 * 60 * 24);
var days = Math.round(diffDate);
});
});
回答by sylenix
This is how I did it using the Math.floor() function:
这就是我使用 Math.floor() 函数的方式:
var start = new Date($('#start').val());
var end = new Date($('#end').val());
var diff = Math.floor((end-start) / (365.25 * 24 * 60 * 60 * 1000));
console.log(diff);
You could also do it this way using the Math.round() function:
您也可以使用 Math.round() 函数以这种方式执行此操作:
var start = new Date($('#start').val());
var end = new Date($('#end').val());
var diff = new Date(end - start) / (1000 * 60 * 60 * 24 * 365.25);
var age = Math.round(diff);
console.log(age);
回答by Adnan Cindioglu
**This is a simple way of getting the DAYS between two dates**
var d1 = moment($("#StartDate").data("DateTimePicker").date());
var d2 = moment($("#EndDate").data("DateTimePicker").date());
var diffInDays = d2.diff(d1, 'days');
if (diffInDays > 0)
{
$("#Total").val(diffInDays);
}
else
{
$("#Total").val(0);
}
回答by Ravi Ram
This is a simple way of getting the DAYS between two dates
这是获取两个日期之间的 DAYS 的简单方法
var nites = dateDiff(arriveDate,departDate,"D");