Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
122
rated 0 times [  123] [ 1]  / answers: 1 / hits: 24845  / 13 Years ago, mon, october 31, 2011, 12:00:00

I'm trying to get the working days between two date.
Example: stdate = 28/10/2011 and endate = 04/11/2011. This should be 6 working days, but its only giving 5 days.



var workingdays = 0;
var weekday = new Array(7);
weekday[0]=Sunday;
weekday[1]=Monday;
weekday[2]=Tuesday;
weekday[3]=Wednesday;
weekday[4]=Thursday;
weekday[5]=Friday;
weekday[6]=Saturday;

while (stdate <= endate)
{
var day = weekday[stdate.getDay()];
if(day != Saturday && day != Sunday)
{
workingdays++;
}
console.log(weekday[stdate.getDay()]);
stdate = new Date(stdate.getTime() + 86400000);
}


The console log shows the results below.



Friday
Saturday
Sunday
Sunday
Monday
Tuesday
Wednesday
Thursday


Sunday shows twice for some reason. Any help would be appreciated.


More From » javascript

 Answers
7

Daylight savings time.



As you are adding 24 hours to the date, it's not enough to get you to the next day on the sunday, as that particular sunday has 25 hours.



You should add a day instead of adding hours:



stdate = new Date(stdate.getFullYear(), stdate.getMonth(), stdate.getDate() + 1);


Explanation: When you call the Date constructor with a date that is out of range, it will automaticall wrap to the next month, i.e. new Date(2010,9,32) gives you the first of November.


[#89365] Friday, October 28, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anikas

Total Points: 258
Total Questions: 102
Total Answers: 95

Location: Monaco
Member since Sun, Jan 16, 2022
2 Years ago
anikas questions
;