UNPKG

@fremtind/jkl-formatters-util

Version:
26 lines (25 loc) 865 B
function formatDate(date) { const day = "".concat(date.getDate()).padStart(2, "0"); const month = "".concat(date.getMonth() + 1).padStart(2, "0"); return "".concat(day, ".").concat(month, ".").concat(date.getFullYear()); } const DATE_REGEX = { // Tillater datoer på formene DDMMYY og DDMMYYYY full: /^(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[0-2])(\d{2}|\d{4})$/, partial: /^(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[0-2])?(\d{1,4})?$/ }; function formatDateString(input, options) { const strippedInput = input.replace(/\D/g, ""); const regex = (options == null ? void 0 : options.partial) ? DATE_REGEX.partial : DATE_REGEX.full; const match = strippedInput.match(regex); if (!match) { return input; } return match.slice(1).filter(Boolean).join("."); } export { DATE_REGEX, formatDate, formatDateString }; //# sourceMappingURL=formatDate.js.map