Reputation: 147
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.
Upvotes: 4
Views: 8526
Reputation: 946
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)
Upvotes: 2
Reputation: 1
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.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() );
}
Upvotes: 0
Reputation: 4765
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);
Upvotes: 0
Reputation: 40497
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.
Upvotes: 1
Reputation: 120516
See http://code.google.com/p/google-caja/source/browse/trunk/src/com/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
Upvotes: 1