请我在如何区分两个日期之间需要您的帮助 (开始日期)22/04/2017-24/04/2018(结束日期),结果为12个月2天。应该在客户端完成。任何帮助
答案 0 :(得分:0)
尝试使用此代码解决您的问题。
var date1 = new Date("22/04/2017");
var date2 = new Date("24/04/2018");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
alert(diffDays);
答案 1 :(得分:0)
var dateDiffInDays = function(startDate, endDate) {
var timeDiff = Date.parse(endDate) - Date.parse(startDate);
return daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
}
console.log(dateDiffInDays("2018-05-01", "2018-06-23"));
答案 2 :(得分:0)
您可以执行以下操作:
var startDate = moment("22/04/2017", "DD/MM/YYYY");
var endDate = moment("24/04/2018", "DD/MM/YYYY");
var months = endDate.diff(startDate, 'months');
var days = endDate.subtract(months, 'months');
var diff = months + ' months ' + days.diff(startDate, 'days') + ' days';
$('#result').html(diff);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.js"></script>
<div id="result"></div>