javascript - How to get number of days in a month excluding weekends - Stack Overflow

admin2025-04-19  0

I have written a function to give me all days of a month excluding weekends. Every thing works fine but when I want to get December days the Date object returns 01 Jan of next week and the function returns an empty array.

Any help please.

function getDaysInMonth(month, year) {
    var date = new Date(year, month-1, 1);
    var days = [];
    while (date.getMonth() === month) {

        // Exclude weekends
        var tmpDate = new Date(date);            
        var weekDay = tmpDate.getDay(); // week day
        var day = tmpDate.getDate(); // day

        if (weekDay !== 1 && weekDay !== 2) {
            days.push(day);
        }

        date.setDate(date.getDate() + 1);
    }

    return days;
}  

alert(getDaysInMonth(month, year))

I have written a function to give me all days of a month excluding weekends. Every thing works fine but when I want to get December days the Date object returns 01 Jan of next week and the function returns an empty array.

Any help please.

function getDaysInMonth(month, year) {
    var date = new Date(year, month-1, 1);
    var days = [];
    while (date.getMonth() === month) {

        // Exclude weekends
        var tmpDate = new Date(date);            
        var weekDay = tmpDate.getDay(); // week day
        var day = tmpDate.getDate(); // day

        if (weekDay !== 1 && weekDay !== 2) {
            days.push(day);
        }

        date.setDate(date.getDate() + 1);
    }

    return days;
}  

alert(getDaysInMonth(month, year))
Share edited Apr 9, 2019 at 12:07 Fury asked Sep 2, 2016 at 15:34 FuryFury 4,7767 gold badges53 silver badges82 bronze badges 5
  • 2 date.getMonth() === month ... getMOnth returns 0 to 11 but you've called the function with month === 12 ... you obviously know that you have to subrtract one from the month :p – Jaromanda X Commented Sep 2, 2016 at 15:35
  • also, weekends are where getDay() == 6 or getDay() == 0 – Jaromanda X Commented Sep 2, 2016 at 15:37
  • @isherwood - yes, they do – Jaromanda X Commented Sep 2, 2016 at 15:38
  • Possible duplicate of Javascript getUTCMonth() returns 0 for December? – Blazemonger Commented Sep 2, 2016 at 15:40
  • @Blazemonger - close, but not really – Jaromanda X Commented Sep 2, 2016 at 15:42
Add a ment  | 

1 Answer 1

Reset to default 7

When you create a date, you use month = 0 to 11

This also goes for when you GET the month - it also returns 0 to 11

I'm surprised you said

Every thing works fine

It actually never worked for any month - always would return an empty array

function getDaysInMonth(month, year) {
    month--; // lets fix the month once, here and be done with it
    var date = new Date(year, month, 1);
    var days = [];
    while (date.getMonth() === month) {

        // Exclude weekends
        var tmpDate = new Date(date);            
        var weekDay = tmpDate.getDay(); // week day
        var day = tmpDate.getDate(); // day

        if (weekDay%6) { // exclude 0=Sunday and 6=Saturday
            days.push(day);
        }

        date.setDate(date.getDate() + 1);
    }

    return days;
}  

alert(getDaysInMonth(month, year))
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745072254a283354.html

最新回复(0)