Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/react-calendar. Ultimate calendar for your React app. projects.wojtekmaj.pl/react-calendar
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458import { getYear, getMonth as getMonthIndex, getCenturyStart, getPreviousCenturyStart, getNextCenturyStart, getCenturyEnd, getPreviousCenturyEnd, getCenturyRange, getDecadeStart, getPreviousDecadeStart, getNextDecadeStart, getDecadeEnd, getPreviousDecadeEnd, getDecadeRange, getYearStart, getPreviousYearStart, getNextYearStart, getYearEnd, getPreviousYearEnd, getYearRange, getMonthStart, getPreviousMonthStart, getNextMonthStart, getMonthEnd, getPreviousMonthEnd, getMonthRange, getDayStart, getDayEnd, getDayRange,} from '@wojtekmaj/date-utils';
import { CALENDAR_TYPES, WEEKDAYS } from './const';import { formatYear as defaultFormatYear } from './dateFormatter';
import type { CalendarType, IntlWeekInfo, RangeType } from './types';
const SUNDAY = WEEKDAYS[0];const FRIDAY = WEEKDAYS[5];const SATURDAY = WEEKDAYS[6];
/* Simple getters - getting a property of a given point in time */
function getWeekInfo(locale: string | undefined, calendarType: CalendarType): IntlWeekInfo { try { if (!locale) { throw new Error('Locale must be specified.'); }
const intlLocale = new Intl.Locale(locale, { calendar: calendarType });
if ('getWeekInfo' in intlLocale) { return (intlLocale.getWeekInfo as () => IntlWeekInfo)(); }
if ('weekInfo' in intlLocale) { return intlLocale.weekInfo as IntlWeekInfo; } } catch (error) { // Continue to fallback logic }
switch (calendarType) { case CALENDAR_TYPES.ISO_8601: return { firstDay: 1, minimalDays: 4, weekend: [6, 7] }; case CALENDAR_TYPES.ISLAMIC: return { firstDay: 6, minimalDays: 1, weekend: [5, 6] }; case CALENDAR_TYPES.HEBREW: return { firstDay: 1, minimalDays: 1, weekend: [6, 7] }; case CALENDAR_TYPES.GREGORY: return { firstDay: 7, minimalDays: 1, weekend: [6, 7] }; default: throw new Error('Unsupported calendar type.'); }}
/** * Gets day of the week of a given date. * @param {Date} date Date. * @param {string} [locale] Locale. * @param {CalendarType} [calendarType="iso8601"] Calendar type. * @returns {number} Day of the week. */export function getDayOfWeek( date: Date, locale?: string, calendarType: CalendarType = CALENDAR_TYPES.ISO_8601,): number { const weekday = date.getDay();
const { firstDay } = getWeekInfo(locale, calendarType);
return (weekday + 7 - firstDay) % 7;}
/** * Century */
/** * Gets the year of the beginning of a century of a given date. * @param {Date} date Date. * @returns {number} Year of the beginning of a century. */export function getBeginOfCenturyYear(date: Date): number { const beginOfCentury = getCenturyStart(date); return getYear(beginOfCentury);}
/** * Decade */
/** * Gets the year of the beginning of a decade of a given date. * @param {Date} date Date. * @returns {number} Year of the beginning of a decade. */export function getBeginOfDecadeYear(date: Date): number { const beginOfDecade = getDecadeStart(date); return getYear(beginOfDecade);}
/** * Week */
/** * Returns the beginning of a given week. * * @param {Date} date Date. * @param {string} [locale] Locale. * @param {CalendarType} [calendarType="iso8601"] Calendar type. * @returns {Date} Beginning of a given week. */export function getBeginOfWeek( date: Date, locale?: string, calendarType: CalendarType = CALENDAR_TYPES.ISO_8601,): Date { const year = getYear(date); const monthIndex = getMonthIndex(date); const day = date.getDate() - getDayOfWeek(date, locale, calendarType); return new Date(year, monthIndex, day);}
/** * Gets week number according to ISO 8601 or US standard. * In ISO 8601, Arabic and Hebrew week 1 is the one with January 4. * In US calendar week 1 is the one with January 1. * * @param {Date} date Date. * @param {string} [locale] Locale. * @param {CalendarType} [calendarType="iso8601"] Calendar type. * @returns {number} Week number. */export function getWeekNumber( date: Date, locale?: string, calendarType: CalendarType = CALENDAR_TYPES.ISO_8601,): number { const calendarTypeForWeekNumber = calendarType === CALENDAR_TYPES.GREGORY ? CALENDAR_TYPES.GREGORY : CALENDAR_TYPES.ISO_8601; const beginOfWeek = getBeginOfWeek(date, locale, calendarType); let year = getYear(date) + 1; let dayInWeekOne; let beginOfFirstWeek;
// Look for the first week one that does not come after a given date do { dayInWeekOne = new Date(year, 0, calendarTypeForWeekNumber === CALENDAR_TYPES.ISO_8601 ? 4 : 1); beginOfFirstWeek = getBeginOfWeek(dayInWeekOne, locale, calendarType); year -= 1; } while (date < beginOfFirstWeek);
return Math.round((beginOfWeek.getTime() - beginOfFirstWeek.getTime()) / (8.64e7 * 7)) + 1;}
/** * Others */
/** * Returns the beginning of a given range. * * @param {RangeType} rangeType Range type (e.g. 'day') * @param {Date} date Date. * @returns {Date} Beginning of a given range. */export function getBegin(rangeType: RangeType, date: Date): Date { switch (rangeType) { case 'century': return getCenturyStart(date); case 'decade': return getDecadeStart(date); case 'year': return getYearStart(date); case 'month': return getMonthStart(date); case 'day': return getDayStart(date); default: throw new Error(`Invalid rangeType: ${rangeType}`); }}
/** * Returns the beginning of a previous given range. * * @param {RangeType} rangeType Range type (e.g. 'day') * @param {Date} date Date. * @returns {Date} Beginning of a previous given range. */export function getBeginPrevious(rangeType: RangeType, date: Date): Date { switch (rangeType) { case 'century': return getPreviousCenturyStart(date); case 'decade': return getPreviousDecadeStart(date); case 'year': return getPreviousYearStart(date); case 'month': return getPreviousMonthStart(date); default: throw new Error(`Invalid rangeType: ${rangeType}`); }}
/** * Returns the beginning of a next given range. * * @param {RangeType} rangeType Range type (e.g. 'day') * @param {Date} date Date. * @returns {Date} Beginning of a next given range. */export function getBeginNext(rangeType: RangeType, date: Date): Date { switch (rangeType) { case 'century': return getNextCenturyStart(date); case 'decade': return getNextDecadeStart(date); case 'year': return getNextYearStart(date); case 'month': return getNextMonthStart(date); default: throw new Error(`Invalid rangeType: ${rangeType}`); }}
export function getBeginPrevious2(rangeType: RangeType, date: Date): Date { switch (rangeType) { case 'decade': return getPreviousDecadeStart(date, -100); case 'year': return getPreviousYearStart(date, -10); case 'month': return getPreviousMonthStart(date, -12); default: throw new Error(`Invalid rangeType: ${rangeType}`); }}
export function getBeginNext2(rangeType: RangeType, date: Date): Date { switch (rangeType) { case 'decade': return getNextDecadeStart(date, 100); case 'year': return getNextYearStart(date, 10); case 'month': return getNextMonthStart(date, 12); default: throw new Error(`Invalid rangeType: ${rangeType}`); }}
/** * Returns the end of a given range. * * @param {RangeType} rangeType Range type (e.g. 'day') * @param {Date} date Date. * @returns {Date} End of a given range. */export function getEnd(rangeType: RangeType, date: Date): Date { switch (rangeType) { case 'century': return getCenturyEnd(date); case 'decade': return getDecadeEnd(date); case 'year': return getYearEnd(date); case 'month': return getMonthEnd(date); case 'day': return getDayEnd(date); default: throw new Error(`Invalid rangeType: ${rangeType}`); }}
/** * Returns the end of a previous given range. * * @param {RangeType} rangeType Range type (e.g. 'day') * @param {Date} date Date. * @returns {Date} End of a previous given range. */export function getEndPrevious(rangeType: RangeType, date: Date): Date { switch (rangeType) { case 'century': return getPreviousCenturyEnd(date); case 'decade': return getPreviousDecadeEnd(date); case 'year': return getPreviousYearEnd(date); case 'month': return getPreviousMonthEnd(date); default: throw new Error(`Invalid rangeType: ${rangeType}`); }}
export function getEndPrevious2(rangeType: RangeType, date: Date): Date { switch (rangeType) { case 'decade': return getPreviousDecadeEnd(date, -100); case 'year': return getPreviousYearEnd(date, -10); case 'month': return getPreviousMonthEnd(date, -12); default: throw new Error(`Invalid rangeType: ${rangeType}`); }}
/** * Returns an array with the beginning and the end of a given range. * * @param {RangeType} rangeType Range type (e.g. 'day') * @param {Date} date Date. * @returns {Date[]} Beginning and end of a given range. */export function getRange(rangeType: RangeType, date: Date): [Date, Date] { switch (rangeType) { case 'century': return getCenturyRange(date); case 'decade': return getDecadeRange(date); case 'year': return getYearRange(date); case 'month': return getMonthRange(date); case 'day': return getDayRange(date); default: throw new Error(`Invalid rangeType: ${rangeType}`); }}
/** * Creates a range out of two values, ensuring they are in order and covering entire period ranges. * * @param {RangeType} rangeType Range type (e.g. 'day') * @param {Date} date1 First date. * @param {Date} date2 Second date. * @returns {Date[]} Beginning and end of a given range. */export function getValueRange(rangeType: RangeType, date1: Date, date2: Date): [Date, Date] { const rawNextValue = [date1, date2].sort((a, b) => a.getTime() - b.getTime()) as [Date, Date]; return [getBegin(rangeType, rawNextValue[0]), getEnd(rangeType, rawNextValue[1])];}
function toYearLabel( locale: string | undefined, formatYear: ((locale: string | undefined, date: Date) => string) | undefined = defaultFormatYear, dates: Date[],): string { return dates.map((date) => formatYear(locale, date)).join(' – ');}
/** * @callback FormatYear * @param {string} locale Locale. * @param {Date} date Date. * @returns {string} Formatted year. */
/** * Returns a string labelling a century of a given date. * For example, for 2017 it will return 2001-2100. * * @param {string} locale Locale. * @param {FormatYear} formatYear Function to format a year. * @param {Date|string|number} date Date or a year as a string or as a number. * @returns {string} String labelling a century of a given date. */export function getCenturyLabel( locale: string | undefined, formatYear: ((locale: string | undefined, date: Date) => string) | undefined, date: Date,): string { return toYearLabel(locale, formatYear, getCenturyRange(date));}
/** * Returns a string labelling a decade of a given date. * For example, for 2017 it will return 2011-2020. * * @param {string} locale Locale. * @param {FormatYear} formatYear Function to format a year. * @param {Date|string|number} date Date or a year as a string or as a number. * @returns {string} String labelling a decade of a given date. */export function getDecadeLabel( locale: string | undefined, formatYear: ((locale: string | undefined, date: Date) => string) | undefined, date: Date,): string { return toYearLabel(locale, formatYear, getDecadeRange(date));}
/** * Returns a boolean determining whether a given date is the current day of the week. * * @param {Date} date Date. * @returns {boolean} Whether a given date is the current day of the week. */export function isCurrentDayOfWeek(date: Date): boolean { return date.getDay() === new Date().getDay();}
/** * Returns a boolean determining whether a given date is a weekend day. * * @param {Date} date Date. * @param {string} [locale] Locale. * @param {CalendarType} [calendarType="iso8601"] Calendar type. * @returns {boolean} Whether a given date is a weekend day. */export function isWeekend( date: Date, locale?: string, calendarType: CalendarType = CALENDAR_TYPES.ISO_8601,): boolean { const weekday = date.getDay();
switch (calendarType) { case CALENDAR_TYPES.ISLAMIC: case CALENDAR_TYPES.HEBREW: return weekday === FRIDAY || weekday === SATURDAY; case CALENDAR_TYPES.ISO_8601: case CALENDAR_TYPES.GREGORY: return weekday === SATURDAY || weekday === SUNDAY; default: throw new Error('Unsupported calendar type.'); }}