如何计算Javascript中两个日期之间的年,月和日差异?

时间:2013-11-27 16:49:33

标签: 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 "

4 个答案:

答案 0 :(得分:1)

天似乎是最棘手的计算,否则它非常简单。从目标毫秒减去当前毫秒数,以获得持续时间(以毫秒为单位)。然后对于每个值但是几天,将持续时间的最低值除以年,月,小时,分钟或秒中的毫秒数。这将为您提供持续时间中的数字或年,月,小时,分钟或秒。最后,取每个值的模数。

连续几天,从持续时间中减去以毫秒为单位的年数和月数,以获得剩余的毫秒数,然后取剩余毫秒的最低值除以一天中的毫秒数。



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




答案 1 :(得分: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

答案 2 :(得分: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;
}

答案 3 :(得分: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;
}