Sariban D'Cl
Sariban D'Cl

Reputation: 2227

Get the number of Sundays of a month using moment.js

I need the total number of sunday's and saturday's of a month. using moment.js library. Can anyone help me. Thanks

Upvotes: 5

Views: 3388

Answers (1)

Markai
Markai

Reputation: 2098

Here is a solution using moment.js:

function getAmountOfWeekDaysInMonth(date, weekday){
    date.date(1);
    var dif = (7 + (weekday - date.weekday()))%7+1;
    console.log("weekday: "+ weekday +", FirstOfMonth: "+ date.weekday() +", dif: "+dif);
    return Math.floor((date.daysInMonth()-dif) / 7)+1;
}

The argument date has to be any date in the month you want analyse.

working fiddle

NOTE: sunday is 0 and saturday 6

Upvotes: 7

Related Questions