, , , OP 2 2011 2 2011 , 10 , : 02:06, 03:06, 09: 06, 10:06, 16:06, 17:06, 23:06, 24:06, 30:06, 31:06.
A way to calculate this, without a loop:
function weekendsBetween(start, end) {
"use strict";
var startDay = start.getDay(),
diff = (end.getTime() - start.getTime() - startDay) / (60000 * 60 * 24),
diffWeaks = (diff / 7) | 0,
remWeaks = Math.ceil(diff % 7), extra = 0;
if (startDay + remWeaks > 7) extra = 2;
else if (startDay + remWeaks == 7 ||
remWeaks > startDay) extra = 1;
return diffWeaks * 2 + extra;
}
var date1 = new Date(2011, 6, 2);
var date2 = new Date(2011, 7, 2);
weekendsBetween(date1, date2);
Please note that this function may not work as you expect if you use it in the server settings (for example, Node.js), because if you do not specify the UTC time zone, it can be translated to your local time and will come down for one day, which can lead to incorrect results.
user797257
source
share