当您减去两个日期对象时,结果是以毫秒为单位的差异。它相当于:
a.getTime() - b.getTime();
如果您想要年、月和日的差异,那是一个不同的主张,因为由于天数的不同(可能比夏令时更改长或短 1 小时),您不能直接转换大的毫秒值,月 (可能是 28 到 31 天(含)和年(闰年和非闰年)。
以下脚本用于计算年龄,但可以轻松适应您的目的:
// Given a date object, calcualte the number of
// days in the month
function daysInMonth(d) {
return (new Date(d.getFullYear(), (d.getMonth() + 1), 0)).getDate();
}
/* For person born on birthDate, return their
** age on datumDate.
**
** Don't modify original date objects
**
** tDate is used as adding and subtracting
** years, months and days from dates on 29 February
** can affect the outcome,
**
** e.g.
**
** 2000-02-29 + 1 year => 2001-03-01
** 2001-03-01 - 1 year => 2000-03-01 so not symetric
**
** Note: in some systems, a person born on 29-Feb
** will have an official birthday on 28-Feb, other
** systems will have official birthday on 01-Mar.
*/
function getAge(birthDate, datumDate) {
// Make sure birthDate is before datumDate
if (birthDate - datumDate > 0) return null;
var dob = new Date(+birthDate),
now = new Date(+datumDate),
tDate = new Date(+dob),
dobY = dob.getFullYear(),
nowY = now.getFullYear(),
years, months, days;
// Initial estimate of years
years = nowY - dobY;
dobY = (dobY + years);
tDate.setYear(dobY);
// Correct if too many
if (now < tDate) {
--years;
--dobY;
}
dob.setYear(dobY);
// Repair tDate
tDate = new Date(+dob);
// Initial month estimate
months = now.getMonth() - tDate.getMonth();
// Adjust if needed
if (months < 0) {
months = 12 + months;
} else if (months == 0 && tDate.getDate() > now.getDate()) {
months = 11;
}
tDate.setMonth(tDate.getMonth() + months);
if (now < tDate) {
--months;
dob.setMonth(tDate.getMonth() - 1);
}
// Repair tDate
tDate = new Date(+dob);
// Initial day estimate
days = now.getDate() - tDate.getDate();
// Adjust if needed
if (days < 0) {
days = days + daysInMonth(tDate);
}
dob.setDate(dob.getDate() + days);
if (now < dob) {
--days;
}
return years + 'y ' + months + 'm ' + days + 'd';
}