fix: validate date object and ensure month and day are within valid ranges

This commit is contained in:
Astrian Zheng 2025-02-20 10:14:56 +11:00
parent 9c6f2fcea3
commit 6c5f1e0c18
Signed by: Astrian
SSH Key Fingerprint: SHA256:rVnhx3DAKjujCwWE13aDl7uV6+9U1MvydLkNRXJrBiA

View File

@ -91,6 +91,13 @@ export default ({ value, onSelect, localization, onClose, mainColor = '#000000',
useEffect(() => { useEffect(() => {
if (!value) return if (!value) return
if (!(value instanceof Date)) {
if (value.year < 100) value.year = Number(`20${value.year}`)
if (value.month < 0 || value.month > 11)
return console.warn('Invalid value: Month should be between 1 and 12.')
if (value.day < 1 || value.day > 31)
return console.warn('Invalid value: Day should be between 1 and 31.')
}
const date = value instanceof Date ? value : new Date(value.year, value.month - 1, value.day) const date = value instanceof Date ? value : new Date(value.year, value.month - 1, value.day)
setSelectedDate(date) setSelectedDate(date)
setCurrentMonth(date.getMonth()) setCurrentMonth(date.getMonth())