2013-04-29 32 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));