Last active
August 10, 2018 15:38
Weeks in month using Moment.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The jsFiddle doesn't seem to be working. Chrome 68