这会在减去时返回错误的日期

时间:2013-04-29 13:43:38

标签: javascript

为什么当我减去这两个日期时,我得到了错误的答案

var a = new Date("1990","0","1");
var b = new Date("1900","0","1");

var x = new Date(a - b);

console.log(x);

answer: Date {Thu Jan 01 1880 02:00:00 GMT+0200 (South Africa Standard Time)}

如何归还:90年零0天0个月

5 个答案:

答案 0 :(得分:2)

查看Moments.js

var start = moment([1990, 0, 1]);
var end = moment([1900, 0, 1]);

console.log(start.from(end));
console.log(start.from(end, true));
console.log(start.diff(end, "years"));

给出

in 90 years
90 years 
90

jsfiddle

要获得您所要求的确切格式,需要更多工作,例如

var start = moment([1990, 0, 1]);
var end = moment([1900, 0, 2]);

var diff = start.diff(end, "years", true);

console.log(Math.floor(diff) + " years and " + Math.floor(30 * ((12 * (diff % 1)) % 1)) + " days and " + Math.floor(12 * (diff % 1)) + " months");

给出

89 years and 29 days and 11 months 

我上面的计算不包括闰年或任何其他差异,这只是粗略的,但你明白了。

jsfiddle

答案 1 :(得分:1)

当您减去两个日期对象时,结果是以毫秒为单位的差异。它相当于:

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';
}

答案 2 :(得分:1)

您的代码存在的问题是x将包含这两个日期之间的差异(以毫秒为单位)。接下来,如果您将其表示为new Date(),它将简单地从日期0(1970年1月1日)中减去它,为您提供您所看到的答案。因此,如果你想获得你可以做的年数:

var x = a-b;
var years = x/1000/60/60/24/365.2425

虽然根据范围内的具体年份不确切,但是在大多数情况下它会这样做。另一方面,如果您需要精确答案或更多功能的工具,您可以使用其他答案提供的第三方库或功能。 (如上所述,moment.js是一个很好的)

答案 3 :(得分:0)

这将为您提供两个日期,月份和日期之间的差异:

function dateDiff(a,b){
    var low = (a>b)?b:a,
    heigh = (a>b)?a:b,
    diff = {
        years:0,
        months:0,
        days:0
    },
    tmpMonth,
    lowDate=low.getDate();
    heighDate=heigh.getDate()
    while(lowDate!==heighDate){
        low.setDate(low.getDate()+1);
        diff.days++;
        if(low>heigh){
          low.setDate(low.getDate()-1);
          diff.days--;
          break;
        }
        lowDate=low.getDate();
    }
    if(low==heigh){return diff;}//a===b no difference
    diff.years=heigh.getFullYear()-low.getFullYear();
    low.setFullYear(low.getFullYear()+diff.years);
    if(low>heigh){
      low.setFullYear(low.getFullYear()-1);
      diff.years--;
    }
    tmpMonth=heigh.getMonth()-low.getMonth();
    diff.months=(tmpMonth<0)?tmpMonth+12:tmpMonth;
    low.setMonth(low.getMonth()+diff.months);
    if(low>heigh){
      low.setMonth(low.getMonth()-1);
      diff.months--;
    }
    return diff;
}


var a = new Date(2001,1,25);//Feb 25
var b = new Date(2001,2,3);
console.log(dateDiff(a,b));
var a = new Date(2000,1,25);//Feb 25
var b = new Date(2000,2,3);
console.log(dateDiff(a,b));
var a = new Date(2000,1,25);//Feb 25
var b = new Date(2001,2,3);
console.log(dateDiff(a,b));

答案 4 :(得分:-1)

我认为-运算符不适用于日期。尝试

var x = new Date(a.getTime() - b.getTime());

根据@Quentin,上述情况不正确。

尽管如此,我认为它不会产生你期望的结果......如果你想要持续时间期间来表示某个时间单位的差异,请查看moment.js

示例:

var a = moment([1990, 1, 1]);
var b = moment([1900, 1, 1]);
b.from(a); // 90 years ago