0

Why when I subtract these two dates do I get the wrong answer

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)}

How do I make it return : 90 years and 0 days and 0 months

4

5 回答 5

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 上

于 2013-04-29T13:51:19.923 回答
1

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

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

虽然这取决于范围内的具体年份并不准确,但在大多数情况下它会做到。另一方面,如果您想要一个精确的答案或更通用的工具,您可以使用其他答案提供的第三方库或函数。(如前所述,moment.js是一个很好的)

于 2013-04-29T13:52:12.480 回答
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';
}
于 2013-04-29T13:50:10.057 回答
0

这将为您提供年、月和日的 2 个日期之间的差异:

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));
于 2013-04-29T14:38:08.450 回答
-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
于 2013-04-29T13:47:42.023 回答