如何計算兩天之間的差額,告訴我餘下的剩餘月份和剩餘天數?如何計算Javascript中兩個日期之間的年,月和日差異?
舉例: 來源:2013年10月1日 爲:2013年11月15日
輸出:1個月和15天
我試過momentjs但它顯示了整個月,天,像1月,45天。 我也試過這個功能,但它顯示了同樣的事情:
var diff = Math.floor(end_date.getTime() - start_date.getTime());
var day = 1000* 60 * 60 * 24;
var days = Math.floor(diff/day);
var months = Math.floor(days/31);
var years = Math.floor(months/12);
var message = days + " days "
message += months + " motnhs "
可能重複[我如何在JavaScript中兩個日期之間的差異?](http://stackoverflow.com/questions/41948/how-do-i-get-在JavaScript中的兩個日期之間的差異) –
似乎它是做你想做的?你期望的輸出是什麼? – theoutlander
這裏是另一個有趣的答案,可能有所幫助 - http://stackoverflow.com/questions/8942895/convert-a-number-of-days-to-days-months-and-years-with-jquery –