is there a way to calculate the number of days between two dates? ...ie numberDays = (date1 - date2).
thanks,
peter.
thanks,
peter.
/*************************************************
EFS Formula : weekdaysBetween
Written By Steve Hare © 2011
written: 7/11/2011
a demo efs that contains a function that will compute the number of weekdays
between two dates. When a date is used, the calculation assumes midnight that day
which is the beginning of the next day, so be aware of the basis of the calculation. I
included some debugPrintln output to illustrate this point.
I also included some debugPrintln output to illustrate the difference in days and the difference in weeks.
I used code at these links to develop this efs:
[url]http://www.breakingpar.com/bkp/home...7256CC30078F008[/url]
[url]http://www.vijayjoshi.org/2008/10/2...-in-javascript/[/url]
[url]http://www.breakingpar.com/bkp/home...725711000753DBF[/url]
[url]http://www.mcfedries.com/JavaScript/DaysBetween.asp[/url]
posted in this thread: \"http://forum.esignalcentral.com/showthread.php?s=&threadid=36337\"
Saved in FileShare \"http://share.esignal.com/groupgo.jsp?groupid=339\"
*************************************************/
function weekdaysBetween(date1, date2) {
var adjust, diffDays, e, endDate, one_day, s, startDate, weeksBetween;
if (date1.indexOf("-") != -1) {
date1 = date1.split("-");
} else if (date1.indexOf("/") != -1) {
date1 = date1.split("/");
} else {
return 0;
}
if (date2.indexOf("-") != -1) {
date2 = date2.split("-");
} else if (date2.indexOf("/") != -1) {
date2 = date2.split("/");
} else {
return 0;
}
if (parseInt(date1[0], 10) >= 1000) {
startDate = new Date(date1[0],date1[1],date1[2]);
} else if (parseInt(date1[2], 10) >= 1000) {
startDate = new Date(date1[2],date1[0],date1[1]);
} else {
return 0;
}
if (parseInt(date2[0], 10) >= 1000) {
endDate = new Date(date2[0],date2[1],date2[2]);
} else if (parseInt(date2[2], 10) >= 1000) {
endDate = new Date(date2[2],date2[0],date2[1]);
} else {
return 0;
}
if (startDate < endDate) {
s = startDate;
e = endDate;
} else {
s = endDate;
e = startDate;
}
one_day = 1000 * 60 * 60 * 24;
diffDays = Math.floor((e - s) / one_day);
weeksBetween = Math.floor(diffDays / 7);
debugPrintln("date1 = "+date1);
debugPrintln("date2 = "+date2);
debugPrintln("startDate = "+s);
debugPrintln("endDate = "+e);debugPrintln("");
debugPrintln("diffDays = "+diffDays);
debugPrintln("weeksBetween = "+weeksBetween);debugPrintln("");
if (s.getDay() == e.getDay()) {
adjust = 0;
} else if (s.getDay() == 0 && e.getDay() == 6) {
adjust = 5;
} else if (s.getDay() == 6 && e.getDay() == 0) {
adjust = 0;
} else if (e.getDay() == 6 || e.getDay() == 0) {
adjust = 5 - s.getDay();
} else if (s.getDay() == 0 || s.getDay() == 6) {
adjust = e.getDay();
} else if (e.getDay() > s.getDay()) {
adjust = e.getDay() - s.getDay();
} else {
adjust = 5 + e.getDay() - s.getDay();
}
return (weeksBetween * 5) + adjust;
}
var num=weekdaysBetween("06/01/2009","07/01/2009");
debugPrintln("num weekdays = "+num);
/*
* debug Output:
*
* date1 = 06,01,2009
* date2 = 07,01,2009
* startDate = Wed Jul 01 2009 00:00:00 GMT-0400 (Eastern Daylight Time)
* endDate = Sat Aug 01 2009 00:00:00 GMT-0400 (Eastern Daylight Time)
*
* diffDays = 31
* weeksBetween = 4
*
* num weekdays = 22
*
*/
Comment