Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 590
Sariban D'Cl Avatar asked Nov 10 '14 12:11

Sariban D'Cl


1 Answers

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

like image 79
Markai Avatar answered Sep 29 '22 07:09

Markai