2013-04-29 71 views
0

为什么当我减去这两个日期我会得到错误的答案错误的日期它返回时减去

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个月

+2

仅供参考,这将是-90不是90 – George 2013-04-29 13:46:05

+0

我编辑的问题 – 2013-04-29 13:47:09

+0

http://stackoverflow.com/questions/5351483/calculate-date-time-difference-in-java – 2013-04-29 13:47:11

回答

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

我不认为-运营商的日期。尝试

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 
+0

'ab'和'a.getTime() - b.getTime()'都产生了-2840140800000 ',所以'-'对'Date'对象很好。 – Quentin 2013-04-29 13:48:41

+0

好吧,那么我会删除@Quentin的那部分答案。谢谢。 – NilsH 2013-04-29 13:50:08

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

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

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

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

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