2011-05-25 123 views
5

需要日期以月为单位的差异。javascript中的月份日期差异

如果我将2011年2月1日的FromDate和2011年4月2日的ToDate给出,则其结果应为2.2。

这意味着它应该先计算月份,然后再计算十进制值中的天数。

看看的时间差为3个月和22天,那么它应该给的差为3.22

是否有可能通过的JavaScript。

+0

有你的问题已经被问过SO这么多相关的问题。请参阅相关章节。 – 2011-05-25 05:40:44

+0

2011年2月1日至2011年4月2日是2.1个月吗? – Dancrumb 2011-05-25 05:41:53

+2

去这里http://www.w3schools.com/js/js_obj_date.asp并做数学 – Ibu 2011-05-25 05:45:19

回答

2
var date1 = new Date(2011,1,1); 
var date2 = new Date(2011,3,2); 
var diffYears = date2.getFullYear()-date1.getFullYear(); 
var diffMonths = date2.getMonth()-date1.getMonth(); 
var diffDays = date2.getDate()-date1.getDate(); 

var months = (diffYears*12 + diffMonths); 
if(diffDays>0) { 
    months += '.'+diffDays; 
} else if(diffDays<0) { 
    months--; 
    months += '.'+(new Date(date2.getFullYear(),date2.getMonth(),0).getDate()+diffDays); 
} 
alert(months); 
1

用法示例不准确,但你可以得到一个粗略的估计与此:

var d=new Date(2010,00,01); //start date 
var dd=new Date(2010,11,31); //end date 
var dif=dd-d //get difference in milliseconds 
var m=dif/(86400000*30); //1000*60*60*24=>86400000 gives one day 

它给12.133〜上面的日期。下面

0

的例子给出了导致2.2日期

var d1 = new Date(2011, 1, 1); 
var d2 = new Date(2011, 3, 2); 

var ydiff = d2.getYear() - d1.getYear(); 
var mdiff = d2.getMonth() - d1.getMonth(); 
var ddiff = 1 + d2.getDate() - d1.getDate(); 

var diff = (ydiff*12 + mdiff + ".") + ddiff; 
alert(diff); 
0

我希望它帮助。 100%的作品。

var startDate = '13.06.2013'; // for example (i got it from input) 
    var endDate = '12.02.2016'; // for example 
    var temp_sd = explode('.', startDate); 
    var temp_ed = explode('.', endDate); 
    var row_id = $(this).attr('row_id'); 

    var m_arr = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; // 365 days in year 
    var m_arr_vis = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; // 366 days 
    var temp_year = parseInt(temp_sd[2]); 
    var temp_month = parseInt(temp_sd[1]); 
    var daysInM, temp_s, temp_e, m_diff = 0; 
    while(true) { 
     if(temp_month === parseInt(temp_sd[1])) { 
      temp_s = parseInt(temp_sd[0]); 
     } else { 
      temp_s = 1; 
     } 
     if((temp_year%4) === 0) { 
      daysInM = m_arr_vis[temp_month-1]; 
     } else { 
      daysInM = m_arr[temp_month-1]; 
     } 
     if(temp_month === parseInt(temp_ed[1])) { 
      temp_e = parseInt(temp_ed[0]); 
     } else { 
      temp_e = daysInM; 
     } 

     m_diff += (temp_e-temp_s+1)/daysInM; 

     //alert(temp_s+' -> '+temp_e+' . '+temp_month+' . '+temp_year); 
     //alert(temp_e-temp_s+1); 
     //alert(daysInM); 
     if((temp_year === parseInt(temp_ed[2]))&&(temp_month === parseInt(temp_ed[1]))) break; 
     // inc temp_month 
     if(temp_month<12) { 
      temp_month++; 
     } else { 
      temp_month = 1; 
      // inc temp_years 
      temp_year++; 
     } 
    } 

    var months = Number(m_diff).toFixed(7); 

...

function explode(delimiter, string) { // Split a string by string 
    // 
    // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 
    // + improved by: kenneth 
    // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 

    var emptyArray = { 0: '' }; 

    if (arguments.length != 2 
      || typeof arguments[0] == 'undefined' 
      || typeof arguments[1] == 'undefined') 
    { 
      return null; 
    } 

    if (delimiter === '' 
      || delimiter === false 
      || delimiter === null) 
    { 
      return false; 
    } 

    if (typeof delimiter == 'function' 
      || typeof delimiter == 'object' 
      || typeof string == 'function' 
      || typeof string == 'object') 
    { 
      return emptyArray; 
    } 

    if (delimiter === true) { 
      delimiter = '1'; 
    } 

    return string.toString().split (delimiter.toString()); 
}