MCPcopy Create free account
hub / github.com/Hacker0x01/react-datepicker / parseDate

Function parseDate

src/date_utils.ts:296–336  ·  view source on GitHub ↗
(
  value: string,
  dateFormat: string | string[],
  locale: Locale | undefined,
  strictParsing: boolean,
  refDate: Date = newDate(),
)

Source from the content-addressed store, hash-verified

294 * @returns - The parsed date or null.
295 */
296export 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.

Callers 3

DatePickerClass · 0.90

Calls 5

newDateFunction · 0.85
getLocaleObjectFunction · 0.85
getDefaultLocaleFunction · 0.85
isValidFunction · 0.85
formatDateFunction · 0.85

Tested by

no test coverage detected