( value: string, dateFormat: string | string[], locale: Locale | undefined, strictParsing: boolean, refDate: Date = newDate(), )
| 294 | * @returns - The parsed date or null. |
| 295 | */ |
| 296 | export function parseDate( |
| 297 | value: string, |
| 298 | dateFormat: string | string[], |
| 299 | locale: Locale | undefined, |
| 300 | strictParsing: boolean, |
| 301 | refDate: Date = newDate(), |
| 302 | ): Date | null { |
| 303 | const localeObject = |
| 304 | getLocaleObject(locale) || getLocaleObject(getDefaultLocale()); |
| 305 | |
| 306 | const formats = Array.isArray(dateFormat) ? dateFormat : [dateFormat]; |
| 307 | |
| 308 | for (const format of formats) { |
| 309 | const parsedDate = parse(value, format, refDate, { |
| 310 | locale: localeObject, |
| 311 | useAdditionalWeekYearTokens: true, |
| 312 | useAdditionalDayOfYearTokens: true, |
| 313 | }); |
| 314 | if ( |
| 315 | isValid(parsedDate) && |
| 316 | (!strictParsing || value === formatDate(parsedDate, format, locale)) |
| 317 | ) { |
| 318 | return parsedDate; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | // When strictParsing is false, try native Date parsing as a fallback |
| 323 | // This allows flexible input formats like "12/05/2025" or "2025-12-16" |
| 324 | // even when the dateFormat prop specifies a different format. |
| 325 | // Only attempt this for inputs that look like complete dates (minimum |
| 326 | // length of 8 characters, e.g., "1/1/2000") to avoid parsing partial |
| 327 | // inputs like "03/" or "2000" which should be handled by parseDateForNavigation. |
| 328 | if (!strictParsing && value && value.length >= 8) { |
| 329 | const nativeDate = new Date(value); |
| 330 | if (isValidDate(nativeDate)) { |
| 331 | return nativeDate; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | return null; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Parses a partial date string for calendar navigation purposes. |
no test coverage detected