2013-11-27 105 views
1

如何計算兩天之間的差額,告訴我餘下的剩餘月份和剩餘天數?如何計算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 " 
+0

可能重複[我如何在JavaScript中兩個日期之間的差異?](http://stackoverflow.com/questions/41948/how-do-i-get-在JavaScript中的兩個日期之間的差異) –

+0

似乎它是做你想做的?你期望的輸出是什麼? – theoutlander

+0

這裏是另一個有趣的答案,可能有所幫助 - http://stackoverflow.com/questions/8942895/convert-a-number-of-days-to-days-months-and-years-with-jquery –

回答

0

你必須做一些估計(一個月31天,一年365天,等等),你必須減去數你」你一直在使用從diff已經使用。

var diff = Math.floor(end_date.getTime() - start_date.getTime()); 
var 
    lengthOfDayInSeconds = 1000* 60 * 60 * 24, 
    lengthOfMonthInSeconds = lengthOfDayInSeconds*31, 
    lengthOfYearInSeconds = lengthOfDayInSeconds*365; 

var yearsBetween = Math.floor(diff/lengthOfYearInSeconds); 
diff -= yearsBetween*lengthOfYearInSeconds; 

var monthsBetween = Math.floor(diff/lengthOfMonthInSeconds); 
diff -= monthsBetween*lengthOfMonthInSeconds; 

var daysBetween = Math.floor(diff/lengthOfDayInSeconds); 

message = yearsBetween + ' years '+ monthsBetween + ' months ' + daysBetween + ' days'; 

1/1/20007/16/2001之間的區別是,通過這個代碼:1 years 6 months 16 days

+0

它的工作原理,但在某些月份,一天的計算可能是錯誤的,因爲一些月份有30天,其他31天和二十八天 – antonioj1015

0

來到這裏的答案: Convert a number (of days) to days, months and years with jQuery

有了這個功能:

function humanise(total_days) 
{ 
    //var total_days = 1001; 
    var date_current = new Date(); 
    var utime_target = date_current.getTime() + total_days*86400*1000; 
    var date_target = new Date(utime_target); 

    var diff_year = parseInt(date_target.getUTCFullYear() - date_current.getUTCFullYear()); 
    var diff_month = parseInt(date_target.getUTCMonth() - date_current.getUTCMonth()); 
    var diff_day = parseInt(date_target.getUTCDate() - date_current.getUTCDate()); 

    var days_in_month = [31, (date_target.getUTCFullYear()%4?29:28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 
    var date_string = ""; 
    while(true) 
    { 
     date_string = ""; 
     date_string += (diff_year>0?diff_year + "Y":""); 

     if(diff_month<0){diff_year -= 1; diff_month += 12; continue;} 
     date_string += (diff_month>0?diff_month + "M":""); 

     if(diff_day<0){diff_month -= 1; diff_day += days_in_month[((11+date_target.getUTCMonth())%12)]; continue;} 
     date_string += (diff_day>0?diff_day + "D":""); 
     break; 
    } 
    return date_string; 
} 
0

我使用這樣才能獲得價值。 modification from this link

function get_number_of_days(firstDate, secondDate) { 
    var diff_year = parseInt(secondDate.getFullYear() - firstDate.getFullYear()); 
    var diff_month = parseInt(secondDate.getMonth() - firstDate.getMonth()); 
    var diff_day = parseInt(secondDate.getDate() - firstDate.getDate()); 

    var hash_date = {}; 

    while(true) { 
    hash_date = {}; 
    hash_date["y"] = diff_year; 

    if(diff_month < 0) { 
     diff_year -= 1; 
     diff_month += 12; 
     continue; 
    } 
    hash_date["m"] = diff_month; 

    if(diff_day < 0) { 
     diff_month -= 1; 
     diff_day += get_month_length(secondDate.getFullYear(), secondDate.getMonth()); 
     continue; 
    } 
    hash_date["d"] = diff_day; 
    break; 
    } 

    return hash_date; 
} 

function get_month_length(year, month) { 
    var hour = 1000 * 60 * 60; 
    var day = hour * 24; 
    var this_month = new Date(year, month, 1); 
    var next_month = new Date(year, month + 1, 1); 
    var length = Math.ceil((next_month.getTime() - this_month.getTime() - hour)/day); 

    return length; 
} 
0

幾天似乎是最棘手的計算,否則它是非常直截了當的。從目標毫秒中減去當前毫秒以獲得以毫秒爲單位的持續時間。然後,除了天數之外,對於每個值,以年,月,小時,分鐘或秒爲單位,持續時間除以毫秒數。這給你的時間內的數字或年,月,小時,分鐘或秒。最後,取每個值的模數。

幾天後,從持續時間中減去以毫秒爲單位的年數和月數,以獲得剩餘的毫秒數,然後將剩餘的毫秒數除以一天中的毫秒數。

function countdown(targetDate) { 
 
    var nowMillis = new Date().getTime(); 
 
    var targetMillis = targetDate.getTime(); 
 
    var duration = targetMillis - nowMillis; 
 
    var years = Math.floor(duration/3.154e+10); 
 
    var durationMinusYears = duration - (years * 3.154e+10); 
 
    var months = Math.floor(duration/2.628e+9) % 12; 
 
    var durationMinusMonths = durationMinusYears - (months * 2.628e+9); 
 
    var days = Math.floor(durationMinusMonths/8.64e+7); 
 
    var hours = Math.floor(duration/3.6e+6) % 24; 
 
    var mins = Math.floor(duration/60000) % 60; 
 
    var secs = Math.floor(duration/1000) % 60; 
 

 
    return [ years, months, days, hours, mins, secs ]; 
 
} 
 

 
console.log('Count down until IE11 is no longer supported => ' + countdown(new Date(2020, 9, 13, 0, 0)));

相關問題