Skip to content

Instantly share code, notes, and snippets.

@faustienf
Last active December 29, 2017 22:18
Show Gist options
  • Save faustienf/593e84fd69cd5b3ad71bc2a928ddfb45 to your computer and use it in GitHub Desktop.
Save faustienf/593e84fd69cd5b3ad71bc2a928ddfb45 to your computer and use it in GitHub Desktop.
Generate matrix for calendar
/**
* Dependencies
* moment
* ramda
*/
/**
*
* @param {string} date
* @param {Object} options
* @param {string} options.activeDay
* @returns {Array[]} matrix
*/
function getCalendarMatrix(dateString, options = {}) {
const date = moment(dateString);
const CURRENT_MONTH = date.month();
const TODAY = moment().format('YYYY-MM-DD');
// reset date
date.date(1).isoWeekday(1).subtract(1, 'day');
return R.range(0, 6).reduce(calendar => {
const weeksOfMonth = R.range(0, 7).map(() => {
date.add(1, 'day');
return {
isCurrentMonth: date.month() === CURRENT_MONTH,
isToday: date.isSame(TODAY),
isActive: date.isSame(options.activeDay),
label: date.format('DD'),
date: date.toString(),
};
});
return R.append(weeksOfMonth, calendar);
}, []);
}
/**
* Example
* getCalendarMatrix('2018-02-15')
*
* 29 30 31 01 02 03 04
* 05 06 07 08 09 10 11
* 12 13 14 15 16 17 18
* 19 20 21 22 23 24 25
* 26 27 28 01 02 03 04
* 05 06 07 08 09 10 11
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment