再次使用 JavaScript 计算两个日期之间的天数

And again about calculate the number of days between two dates using JavaScript

似乎来自 How to calculate the number of days between two dates using JavaScript? 的解决方案 工作正常,但情况除外: 30.4.2016 - 1.5.2016 它计算 2 天(实际 1 天) 29.2.2016 - 1.3.2016 它计算 3 天(实际 1 天)

可能有人已经找到了这些案例的解决方案?

This function from link
function count_days(){ // Expecting resuls: positive when date1 > date2  ; negative  when date1 < date2  
            var $obj = "dd.mm.YYYY";
            var $obj2 = "dd.mm.YYYY";
            if($obj2.value){
                $start=$obj.value.split(".");
                var date1 = new Date($start[2],$start[1],$start[0]); // Converted date to ("YYYY,mm,dd")
                var $stop=$obj2.value.split(".");
                var date2 = new Date($stop[2],$stop[1],$stop[0]); // Converted date to ("YYYY,mm,dd")
                var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
                var diffDays = (date2.getTime() - date1.getTime())/(oneDay);
                if(diffDays >= 0){
                    return Math.abs(diffDays)+1;
                } else {
                    return diffDays-1;
                }
            }
return false; // No date2 nothing to compare  
}
// Expecting resuls in full days (24h):  
//(5.5.2016 - 5.5.2016) =  1 day (days equal)  
//(29.2.2016 - 1.3.2016) =  1 day  
//(30.4.2016 - 1.5.2016) =  1 day  
//(4.5.2016 - 5.5.2016) =  2 days  
//(29.2.2016 - null) = False   
//(1.3.2016 - 29.2.2016) = -1 day  
//(1.5.2016 - 30.4.2016) = -1 day  
//(5.5.2016 - 4.5.2016)  = -2 day  

请注意 new Date() 的月份值从 0 开始(0 - 一月,1 - 二月等...)。

var 
  a = new Date(2016, 01, 29), // Feb 29 2016 00:00:00
  b = new Date(2016, 02, 1); // Mar 01 2016 00:00:00

alert((+b - +a) / 1000 / 60 / 60 / 24); // 1 day

表达式 (+b - +a) - returns 计算两个日期之间的毫秒差(1 秒 = 1000 毫秒)。

结果你可以转换:

  • 毫秒到秒:(+b - +a) / 1000 = 86400 秒

  • 秒到分钟:(+b - +a) / 1000 / 60 = 1440 分钟

  • 分钟到小时:(+b - +a) / 1000 / 60 / 60 = 24 小时

  • 小时到天:(+b - +a) / 1000 / 60 / 60 / 24 = 1 天

+a 和 +b - 为您提供整数值,表示每个日期自 1970 年 1 月 1 日以来的毫秒数 00:00:00。

阅读更多关于 new Date()

The JavaScript date is based on a time value that is milliseconds since midnight 01 January, 1970 UTC. A day holds 86,400,000 milliseconds. The JavaScript Date object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC.

此代码适用于所有情况:

      function fmtDate( when ) {
    function D2( val ) {
      return ( val < 10 ) ? '0' + val : '' + val;
    }
    return D2( when.getMonth() + 1 ) + '/' + D2( when.getDate() ) + '/' + when.getFullYear();
  }

  function workdays( d1, d2 ) {
    var result = 0;
    var d0 = new Date();
    var negative = 1;
    if ( ( typeof( d1 ) == typeof( d2 ) ) && ( typeof( d1 ) == typeof( d0 ) ) ) {
      if ( d2 < d1 ) {  // Exchange/swap the dates
        d0 = d2;
        d2 = d1;
        d1 = d0;
        negative = -1;
      }
      for ( d = d1; d < d2; d.setDate( d.getDate() + 1 ) ) {
       dow = d.getDay();     // Day Of Week: 0 (Sun) .. 6 (Sat)
       when = fmtDate( d );  // Formatted date
        if ( ( dow >= 0 ) && ( dow <= 6 )) {

            result++;
        }
      }
    } else {
      alert( 'workdays() - parameter error - date objects required.' );
    }
    return result
  }

如果您从此行中删除“=”:

if ( ( dow >= 0 ) && ( dow <= 6 ))

你可以用它来计算有多少个工作日(不包括周六和周日)

终于有了好的工作代码

function count_days(){ 
            var $obj = "dd.mm.YYYY";
            var $obj2 = "dd.mm.YYYY";
            if($obj2.value){
                $start=$obj.value.split(".");
                var date1 = new Date($start[2],$start[1]-1,$start[0]); // Converted date to ("YYYY,mm,dd")
                var $stop=$obj2.value.split(".");
                var date2 = new Date($stop[2],$stop[1]-1,$stop[0]); // Converted date to ("YYYY,mm,dd")
                var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
                var diffDays = (date2.getTime() - date1.getTime())/(oneDay);
                if(diffDays >= 0){
                    return Math.abs(diffDays)+1; // positive as date1 > date2    
                } else {
                    return diffDays-1;  //negative  as date1 < date2
                }
            }
return false; // No date2 nothing to compare  
}