UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

83 lines 3.13 kB
import { isBefore, parse, sub } from "date-fns"; import { isValidDate } from "./check-dates.js"; export const INPUT_DATE_STRING_FORMAT_DATE = "dd.MM.yyyy"; export const INPUT_DATE_STRING_FORMAT_MONTH = "MMMM yyyy"; const ALLOWED_INPUT_FORMATS_DATE = [ INPUT_DATE_STRING_FORMAT_DATE, "ddMMyyyy", "dd/MM/yyyy", "dd-MM-yyyy", ]; const ALLOWED_INPUT_FORMATS_MONTH = [ "M/yyyy", "MM/yyyy", "M-yyyy", "MM-yyyy", "MM.yyyy", "MMyyyy", INPUT_DATE_STRING_FORMAT_MONTH, ...ALLOWED_INPUT_FORMATS_DATE, ]; export const parseDate = (date, today, locale, type, allowTwoDigitYear) => { let parsed; const ALLOWED_FORMATS = type === "date" ? ALLOWED_INPUT_FORMATS_DATE : ALLOWED_INPUT_FORMATS_MONTH; if (allowTwoDigitYear) { for (const format of ALLOWED_FORMATS) { parsed = parse(date, format, today, { locale }); if (isValidDate(parsed) && !isTwoDigitYear(date, today, locale, ALLOWED_FORMATS)) { return parsed; } } for (const format of [ ...ALLOWED_FORMATS.map((x) => x.replace("yyyy", "yy")), ]) { parsed = parse(date, format, today, { locale }); if (isValidDate(parsed) && isTwoDigitYear(date, today, locale, ALLOWED_FORMATS)) { const convertedDate = assignCenturyToDate(date, format, today, locale); if (isValidDate(new Date(convertedDate))) { return new Date(convertedDate); } return new Date("Invalid date"); } } return new Date("Invalid date"); } for (const format of ALLOWED_FORMATS) { parsed = parse(date, format, today, { locale }); if (isValidDate(parsed) && !isTwoDigitYear(date, today, locale, ALLOWED_FORMATS)) { return parsed; } } return new Date("Invalid date"); }; function isTwoDigitYear(dateString, today, locale, formats) { let parsed; const newFormat = formats.map((x) => x.replace("yyyy", "yy")); for (const format of newFormat) { parsed = parse(dateString, format, today, { locale }); if (isValidDate(parsed)) { return true; } } return false; } function assignCenturyToDate(dateStr, format, today, locale) { const date20Century = parse(appendCenturyToTwoYearDigitDateString(dateStr, "19"), format.replace("yy", "yyyy"), today, { locale }); const date21Century = parse(appendCenturyToTwoYearDigitDateString(dateStr, "20"), format.replace("yy", "yyyy"), today, { locale }); if (isValidDate(date20Century) && isValidDate(date21Century)) { return isBefore(date20Century, sub(new Date(), { years: 80, })) ? date21Century : date20Century; } return new Date("Invalid date"); } function appendCenturyToTwoYearDigitDateString(dateString, century) { const twoDigitYear = dateString.slice(-2); return `${dateString.slice(0, dateString.length - 2)}${century}${twoDigitYear}`; } //# sourceMappingURL=parse-date.js.map