This script calculates the difference between two dates excluding weekdays. If Saturday and Sunday are there in given dates it will exclude these days and gives the remaining days result.
<html>
<body>
<script type="text/javascript">
var iWeeks, iDateDiff, iAdjust = 0;
var fromDateSplit1 = "2011-08-05 18:31:36.0".split(" "); // Date,mm,Year
var toDateSplit1 = "2011-08-08 18:31:36.0".split(" ");
var fromDateSplit = fromDateSplit1[0].split("-");
var toDateSplit = toDateSplit1[0].split("-");
var startDate = new Date(fromDateSplit[0],fromDateSplit[1]-1,fromDateSplit[2]);
var endDate = new Date(toDateSplit[0],toDateSplit[1]-1,toDateSplit[2]);
alert(startDate);
alert(endDate );
// if (endDate < startDate) return -1; // error code if dates transposed
var iWeekday1 = startDate.getDay(); // day of week
var iWeekday2 = endDate.getDay();
iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; //change Sunday from 0 to 7
iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; //bth days on weekend
iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;
// differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
iWeeks = Math.floor((endDate.getTime() - startDate.getTime()) / 604800000)
if (iWeekday1 <= iWeekday2) {
iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
} else {
iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
}
iDateDiff -= iAdjust // take into account both days on weekend
alert(iDateDiff + 1);
</script>
</body>
</html>
No comments:
Post a Comment