|
import { CalendarDate, CalendarDateTime, ZonedDateTime, parseZonedDateTime, parseDateTime, parseDate, getLocalTimeZone, getDayOfWeek, toCalendar, } from '@internationalized/date'; |
|
const defaultDateDefaults = { |
|
defaultValue: undefined, |
|
defaultPlaceholder: undefined, |
|
granularity: 'day', |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getDefaultDate(props) { |
|
const withDefaults = { ...defaultDateDefaults, ...props }; |
|
const { defaultValue, defaultPlaceholder, granularity } = withDefaults; |
|
if (Array.isArray(defaultValue) && defaultValue.length) { |
|
return defaultValue[defaultValue.length - 1]; |
|
} |
|
if (defaultValue && !Array.isArray(defaultValue)) { |
|
return defaultValue; |
|
} |
|
else if (defaultPlaceholder) { |
|
return defaultPlaceholder; |
|
} |
|
else { |
|
const date = new Date(); |
|
const year = date.getFullYear(); |
|
const month = date.getMonth() + 1; |
|
const day = date.getDate(); |
|
const calendarDateTimeGranularities = ['hour', 'minute', 'second']; |
|
if (calendarDateTimeGranularities.includes(granularity ?? 'day')) { |
|
return new CalendarDateTime(year, month, day, 0, 0, 0); |
|
} |
|
return new CalendarDate(year, month, day); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function parseStringToDateValue(dateStr, referenceVal) { |
|
let dateValue; |
|
if (referenceVal instanceof ZonedDateTime) { |
|
dateValue = parseZonedDateTime(dateStr); |
|
} |
|
else if (referenceVal instanceof CalendarDateTime) { |
|
dateValue = parseDateTime(dateStr); |
|
} |
|
else { |
|
dateValue = parseDate(dateStr); |
|
} |
|
|
|
return dateValue.calendar !== referenceVal.calendar |
|
? toCalendar(dateValue, referenceVal.calendar) |
|
: dateValue; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function toDate(dateValue, tz = getLocalTimeZone()) { |
|
if (dateValue instanceof ZonedDateTime) { |
|
return dateValue.toDate(); |
|
} |
|
else { |
|
return dateValue.toDate(tz); |
|
} |
|
} |
|
export function isCalendarDateTime(dateValue) { |
|
return dateValue instanceof CalendarDateTime; |
|
} |
|
export function isZonedDateTime(dateValue) { |
|
return dateValue instanceof ZonedDateTime; |
|
} |
|
export function hasTime(dateValue) { |
|
return isCalendarDateTime(dateValue) || isZonedDateTime(dateValue); |
|
} |
|
|
|
|
|
|
|
export function getDaysInMonth(date) { |
|
if (date instanceof Date) { |
|
const year = date.getFullYear(); |
|
const month = date.getMonth() + 1; |
|
|
|
|
|
|
|
|
|
|
|
return new Date(year, month, 0).getDate(); |
|
} |
|
else { |
|
return date.set({ day: 100 }).day; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isBefore(dateToCompare, referenceDate) { |
|
return dateToCompare.compare(referenceDate) < 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isAfter(dateToCompare, referenceDate) { |
|
return dateToCompare.compare(referenceDate) > 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isBeforeOrSame(dateToCompare, referenceDate) { |
|
return dateToCompare.compare(referenceDate) <= 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isAfterOrSame(dateToCompare, referenceDate) { |
|
return dateToCompare.compare(referenceDate) >= 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isBetweenInclusive(date, start, end) { |
|
return isAfterOrSame(date, start) && isBeforeOrSame(date, end); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isBetween(date, start, end) { |
|
return isAfter(date, start) && isBefore(date, end); |
|
} |
|
export function getLastFirstDayOfWeek(date, firstDayOfWeek, locale) { |
|
const day = getDayOfWeek(date, locale); |
|
if (firstDayOfWeek > day) { |
|
return date.subtract({ days: day + 7 - firstDayOfWeek }); |
|
} |
|
if (firstDayOfWeek === day) { |
|
return date; |
|
} |
|
return date.subtract({ days: day - firstDayOfWeek }); |
|
} |
|
export function getNextLastDayOfWeek(date, firstDayOfWeek, locale) { |
|
const day = getDayOfWeek(date, locale); |
|
const lastDayOfWeek = firstDayOfWeek === 0 ? 6 : firstDayOfWeek - 1; |
|
if (day === lastDayOfWeek) { |
|
return date; |
|
} |
|
if (day > lastDayOfWeek) { |
|
return date.add({ days: 7 - day + lastDayOfWeek }); |
|
} |
|
return date.add({ days: lastDayOfWeek - day }); |
|
} |
|
export function areAllDaysBetweenValid(start, end, isUnavailable, isDisabled) { |
|
if (isUnavailable === undefined && isDisabled === undefined) { |
|
return true; |
|
} |
|
let dCurrent = start.add({ days: 1 }); |
|
if (isDisabled?.(dCurrent) || isUnavailable?.(dCurrent)) { |
|
return false; |
|
} |
|
const dEnd = end; |
|
while (dCurrent.compare(dEnd) < 0) { |
|
dCurrent = dCurrent.add({ days: 1 }); |
|
if (isDisabled?.(dCurrent) || isUnavailable?.(dCurrent)) { |
|
return false; |
|
} |
|
} |
|
return true; |
|
} |
|
|