Skip to content

Commit 7408a8c

Browse files
committed
feat: optional range util
1 parent 8653ccd commit 7408a8c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/Date/dateUtils.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
102138
export function isLeapYear({ year }: { year: number }) {
103139
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0
104140
}

0 commit comments

Comments
 (0)