File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -99,6 +99,42 @@ export function isDateBetween(
9999 return date <= endDate && date >= startDate
100100}
101101
102+ /**
103+ * Check if a date is within an optional range.
104+ *
105+ * If the range doesn't exist, it defaults to `true`.
106+ */
107+ export function isDateWithinOptionalRange (
108+ date : Date ,
109+ {
110+ startDate,
111+ endDate,
112+ } : { startDate ?: Date | null | undefined ; endDate ?: Date | null | undefined }
113+ ) {
114+ if ( startDate ) {
115+ // if we're on the same day, we're within the valid range (inclusive)
116+ const isSameDay = areDatesOnSameDay ( startDate , date )
117+ const isBeforeMinDate = date < startDate
118+
119+ if ( ! isSameDay && isBeforeMinDate ) {
120+ // disable the selection
121+ return false
122+ }
123+ }
124+
125+ if ( endDate ) {
126+ // if we're on the same day, we're within the valid range (inclusive)
127+ const isSameDay = areDatesOnSameDay ( endDate , date )
128+ const isAfterMaxDate = date > endDate
129+
130+ if ( ! isSameDay && isAfterMaxDate ) {
131+ return false
132+ }
133+ }
134+
135+ return true
136+ }
137+
102138export function isLeapYear ( { year } : { year : number } ) {
103139 return ( year % 4 === 0 && year % 100 !== 0 ) || year % 400 === 0
104140}
You can’t perform that action at this time.
0 commit comments