Instantly share code, notes, and snippets.

Embed
What would you like to do?
Weeks in month using Moment.js
var get_calendar;
var calendar, endDay, firstDay, firstWeekDay, headerRow, i, j, lastWeekDay, len, len1, month, monthRange, row, startDate, week, weekRange, weeks, year,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
get_calendar = function(year, month) {
startDate = moment([year, month]);
firstDay = moment(startDate).startOf('month');
endDay = moment(startDate).endOf('month');
monthRange = moment.range(firstDay, endDay);
weeks = [];
monthRange.by('days', function(moment) {
var ref;
ref = void 0;
if (ref = moment.week()) {
indexOf.call(weeks, ref) < 0;
return weeks.push(moment.week());
}
});
calendar = [];
i = 0;
len = weeks.length;
while (i < len) {
week = weeks[i];
if (i > 0 && week < weeks[i - 1]) {
firstWeekDay = moment([year, month]).add(1, 'year').week(week).day(1);
lastWeekDay = moment([year, month]).add(1, 'year').week(week).day(7);
} else {
firstWeekDay = moment([year, month]).week(week).day(1);
lastWeekDay = moment([year, month]).week(week).day(7);
}
weekRange = moment.range(firstWeekDay, lastWeekDay);
calendar.push(weekRange);
i++;
}
return calendar;
};
@antoniobrandao

This comment has been minimized.

antoniobrandao commented Aug 10, 2018

The jsFiddle doesn't seem to be working. Chrome 68

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment