Im trying to calculate the # of days between 2 dates (the current date and whatever date the user enters)
http://fixmysite.us/Web_Programming_With_Javascriipt/Assignment9/
I store both values in a variable (current_date, input_date) but am having trouble with this?
got it, what I did was convert the difference between the two dates in milliseconds/ then divide it by the number of milliseconds in 1 day, that gave me a decimal point number, then I used Math.floor() to make it an integer
Did you want just the total days between the dates or did you want business days?
var dayDiff = function(d1,d2) {var d= Math.abs(d1-d2);return Math.floor(d/(86400000));};
var busDayDiff = function(d1, d2) {
var d = dayDiff(d1,d2);
var t= d%7;
if (d1 < d2) {w1= d1.getDay(); w2= d2.getDay();}
else {w2= d1.getDay(); w1= d2.getDay();}
if (w1 > w2) t-=2
;if ((w1 == 0 && w2 == 6) || w1 == 6) t--;
return Math.abs((Math.floor(d/7)*5)+t);
};
the total # of days, this ok"?
//find the # of ms in 1 day
var oneDay = 24*60*60*1000;
// if you date is in good format, it comes in like mm/dd/yyyy so I used substring() to break it up to day, month,year
var userDate = new Date(userYear,userMonth,userDay);
//then I use this formula to find out days between both
var diffDays = Math.abs((userDate.getTime() - currentDate.getTime())/(oneDay));
diffDays = Math.floor(diffDays);
the top line of the code I posted is about the shortest code to do that - assuming you have thedates in proper date fields to start with.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.