admin管理员组文章数量:1355600
Need date Difference in terms of months.
If I give the FromDate as 1st Feb,2011 and ToDate as 2nd April 2011 then Its should give result as 2.2.
That means It should calculate the months first then the number of days in the decimal value.
See if date difference is 3 months and 22 days then It should give the difference as 3.22
Is it possible through the Javascript.
Need date Difference in terms of months.
If I give the FromDate as 1st Feb,2011 and ToDate as 2nd April 2011 then Its should give result as 2.2.
That means It should calculate the months first then the number of days in the decimal value.
See if date difference is 3 months and 22 days then It should give the difference as 3.22
Is it possible through the Javascript.
Share Improve this question asked May 25, 2011 at 5:35 InduIndu 1472 gold badges4 silver badges9 bronze badges 7- there are so many related question to your question already been asked on SO. See related section. – Harry Joy Commented May 25, 2011 at 5:40
- Shouldn't 1st Feb 2011 to 2nd April 2011 be 2.1 months? – Dancrumb Commented May 25, 2011 at 5:41
- 2 go here w3schools./js/js_obj_date.asp and do the math – Ibu Commented May 25, 2011 at 5:45
- 1 see dateJS – dexter Commented May 25, 2011 at 5:46
- @Ates Goral of course stackoverflow :D – TheVillageIdiot Commented May 25, 2011 at 6:06
5 Answers
Reset to default 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);
}
console.log(months)
See http://code.google./p/google-caja/source/browse/trunk/src//google/caja/demos/calendar/time-cajita.js# for an RFC 2445 date/time library.
time.durationBetween
returns a duration value that is the difference between two dates.
You can see usage examples in the unittests
not exact but you can get a rough estimate with this:
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
It gives 12.133~ for above dates.
the example below gives result 2.2 for your dates
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);
I hope that it helps. 100% works.
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)
// + improved by: kenneth
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld)
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() );
}
本文标签:
版权声明:本文标题:date difference in months in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743961719a2569152.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论