javascript中以月为单位的日期差异
需要以月为单位的日期差异 如果我给出的起始日期为2011年2月1日,截止日期为2011年4月2日,则其结果应为2.2 这意味着它应该先计算月份,然后计算十进制值中的天数 如果日期差为3个月22天,则应给出3.22天的差值 是否可以通过Javascript实现。有关RFC2445日期/时间库,请参见javascript中以月为单位的日期差异,javascript,Javascript,需要以月为单位的日期差异 如果我给出的起始日期为2011年2月1日,截止日期为2011年4月2日,则其结果应为2.2 这意味着它应该先计算月份,然后计算十进制值中的天数 如果日期差为3个月22天,则应给出3.22天的差值 是否可以通过Javascript实现。有关RFC2445日期/时间库,请参见 time.durationBetween返回两个日期之差的持续时间值 您可以在中看到使用示例不准确,但您可以通过以下内容获得粗略估计: var d=new Date(2010,00,01); //st
time.durationBetween
返回两个日期之差的持续时间值
您可以在中看到使用示例不准确,但您可以通过以下内容获得粗略估计:
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~。下面的示例给出了日期的结果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);
var date1=新日期(2011,1,1);
var date2=新日期(2011,3,2);
var diffYears=date2.getFullYear()-date1.getFullYear();
var diffMonths=date2.getMonth()-date1.getMonth();
var diffDays=date2.getDate()-date1.getDate();
var月数=(diffYears*12+diffMonths);
如果(diffDays>0){
月+='..+天;
}否则,如果(diffDays我希望它有帮助。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);
关于你的问题,已经有很多相关的问题被问到了。请看相关部分。2011年2月1日到2011年4月2日不是应该是2.1个月吗?到这里来,做一个mathsee@Ates Goral当然是stackoverflow:我知道这是一个非常古老的答案,但请记住,它是错误的。它不适合date1的月份变大。但是年份变大了比date2的小。这确实是一个非常古老的答案。但我无法重现您提到的问题。如果date1的月份大于date2的月份,则diffMonths将为负值,并且数学仍然很好。我遗漏了什么吗?哈哈,不,您没有遗漏什么,看起来我遗漏了。感谢您的回复。
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() );
}