luxon#Interval JavaScript Examples
The following examples show how to use
luxon#Interval.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: calendar.js From jc-calendar with MIT License | 5 votes |
/**
* Returns an array for the passed date's month with trailing dates for next/previous months.
* The array represents a calendar that starts on Sunday and ends on Saturday.
* @param {string} dateString The date (format: `yyyy-MM`) to generate the grid from.
*/
export function getMonthlyCalendarGrid(dateString) {
const month = DateTime.fromFormat(dateString, MONTH_FORMAT);
// Get the interval for the provided month
const monthInterval = Interval.fromDateTimes(
month.startOf('month'),
month.endOf('month')
);
// Get offsets for trailing months
const firstWeekOffset = toLocalWeekdayNumber(monthInterval.start.weekday) - 1;
const lastWeekOffset =
DAYS_IN_A_WEEK - toLocalWeekdayNumber(monthInterval.end.weekday);
// Get calendar with trailing intervals
const calendarInterval = Interval.fromDateTimes(
monthInterval.start.minus({
days: firstWeekOffset > 0 ? firstWeekOffset : 0,
}),
monthInterval.end.plus({ days: lastWeekOffset })
);
// Map the interval to an ordered dates array that represents a calendars month.
const totalDays = calendarInterval.count('days');
const start = calendarInterval.start;
return Array(totalDays)
.fill(null)
.map((_, startOffset) => {
const date = start.plus({ days: startOffset });
return {
key: date.toFormat(DATE_FORMAT),
text: date.toLocaleString({ locale: APP_LOCALE, day: 'numeric' }),
trailing: !month.hasSame(date, 'month'),
isWeekend: weekendNumbers.includes(toLocalWeekdayNumber(date.weekday)),
};
});
}