我在使用JS函数时遇到了麻烦。 我从这里获得了这个JS代码Difference between two dates in years, months, days in JavaScript
这几个月的工作正常,但是当我试着打电话给那些似乎工作正常的日子时,它会显示30天应该是24天。有人能看到代码有什么问题吗?
function monthDiff(d1, d2) {
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth() + 1;
months += d2.getMonth();
return months <= 0 ? 0 : months;
}
function daysInMonth(date) {
return new Date(date.getYear(), date.getMonth() + 1, 0).getDate();
}
function diffDate(date1, date2) {
if (date2 && date2.getTime() && !isNaN(date2.getTime())) {
var months = monthDiff(d1, d2);
var days = 0;
if (date1.getUTCDate() >= date2.getUTCDate()) {
days = date1.getUTCDate() - date2.getUTCDate();
}
else {
months--;
days = date1.getUTCDate() - date2.getUTCDate() + daysInMonth(date2);
}
// Use the variables months and days how you need them.
}
}
这里是使用函数的var
var months = monthDiff(new Date(), options.timestamp ) % 12 + 1,
days = daysInMonth(new Date()),
答案 0 :(得分:6)
尝试这个简短,准确的代码:
var _MS_PER_DAY = 1000 * 60 * 60 * 24;
// a and b are javascript Date objects
var a = new Date("7/13/2010");
var b = new Date("12/15/2010");
function dateDiffInDays(a, b) {
// Discard the time and time-zone information.
var utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
return Math.round((utc2 - utc1) / _MS_PER_DAY);
}
答案 1 :(得分:2)
不要再重新发明轮子。
只需插入Moment.js Date Range Plugin。
var starts = moment('2014-02-03 12:53:12');
var ends = moment();
var duration = moment.duration(ends.diff(starts));
// with ###moment precise date range plugin###
// it will tell you the difference in human terms
var diff = moment.preciseDiff(starts, ends, true);
// example: { "years": 2, "months": 7, "days": 0, "hours": 6, "minutes": 29, "seconds": 17, "firstDateWasLater": false }
// or as string:
var diffHuman = moment.preciseDiff(starts, ends);
// example: 2 years 7 months 6 hours 29 minutes 17 seconds
document.getElementById('output1').innerHTML = JSON.stringify(diff)
document.getElementById('output2').innerHTML = diffHuman
&#13;
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
<script src="https://raw.githubusercontent.com/codebox/moment-precise-range/master/moment-precise-range.js"></script>
</head>
<body>
<h2>Difference between "NOW and 2014-02-03 12:53:12"</h2>
<span id="output1"></span>
<br />
<span id="output2"></span>
</body>
</html>
&#13;
答案 2 :(得分:1)
这是一个函数,它将返回以年,月和日数表示的差异:
function dateDiff(d1, d2) {
var years = 0, months = 0, days = 0, arr;
if (d1 > d2) { // call inverse
interval = dateDiff(d2, d1);
return {
years: -interval.years,
months: -interval.months,
days: -interval.days
};
}
days = d2.getDate() - d1.getDate();
if (days < 0) {
months--;
days+= new Date(new Date(d2).setDate(0)).getDate();
}
months += d2.getMonth() - d1.getMonth();
if (months < 0) {
years--;
months+=12;
}
years += d2.getFullYear() - d1.getFullYear();
return {
years: years,
months: months,
days: days
};
}
// Sample data
var d1 = new Date('2013-10-20');
var d2 = new Date('2016-12-16')
// Call
var interval = dateDiff(d1, d2);
// Output each number separately
console.log('Years: ', interval.years);
console.log('Months: ', interval.months);
console.log('Days: ', interval.days);
&#13;