{"record":{"id":"d643da3ef7d292d9","repo":"TheAlgorithms/JavaScript","slug":"invalid-date-format-please-use-dd-mm-yyyy","errorCode":null,"errorMessage":"Invalid date format. Please use 'dd/mm/yyyy'.","messagePattern":"Invalid date format\\. Please use 'dd/mm/yyyy'\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Timing-Functions/ParseDate.js","lineNumber":15,"sourceCode":"import { getMonthDays } from './GetMonthDays'\n\nfunction checkDate(date) {\n  if (date.day < 1 || date.day > getMonthDays(date.month, date.year)) {\n    throw new Error('Invalid day value.')\n  }\n}\n\nfunction parseDate(dateString) {\n  const regex = /^(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})$/\n\n  const match = dateString.match(regex)\n\n  if (!match) {\n    throw new Error(\"Invalid date format. Please use 'dd/mm/yyyy'.\")\n  }\n\n  const res = {\n    day: parseInt(match[1], 10),\n    month: parseInt(match[2], 10),\n    year: parseInt(match[3], 10)\n  }\n  checkDate(res)\n  return res\n}\n\nexport { parseDate }\n","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Timing-Functions/ParseDate.js#L1-L28","documentation":"Thrown by parseDate() in Timing-Functions/ParseDate.js when dateString does not match the regex /^(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})$/ — i.e. it must be one or two digits, a slash, one or two digits, a slash, exactly four digits, with nothing before or after. Generic Error signalling format mismatch. Field order is dd/mm/yyyy (day, month, year) per the parseInt assignments.","triggerScenarios":"Calling parseDate('2021-01-15') (ISO format, dashes, year first), parseDate('1-15-2021') (dashes not slashes), parseDate('15/01/21') (two-digit year, regex requires four), parseDate('15.01.2021') (dots not slashes), parseDate('') (empty), parseDate(12345) (dateString.match would throw on non-string). Note: passing a non-string like a Number will throw a different error (TypeError from .match) rather than this message.","commonSituations":"US-formatted input 'mm/dd/yyyy' looking identical to the expected 'dd/mm/yyyy'; ISO strings from APIs ('2021-01-15'); two-digit years; locale-specific separators (dots in some EU locales, dashes); user typing without separators.","solutions":["Format input as dd/mm/yyyy with slashes and a four-digit year: parseDate('15/01/2021').","If your source is ISO, reformat before calling: const [y,m,d] = iso.split('-'); parseDate(`${d}/${m}/${y}`).","Convert other separators to slashes: s.replace(/[-.]/g, '/').","Pad two-digit years and ensure month/day are 1–2 digits before calling."],"exampleFix":"// before\nparseDate('2021-01-15') // ISO, throws\n\n// after\nconst [y, m, d] = '2021-01-15'.split('-')\nparseDate(`${d}/${m}/${y}`) // '15/01/2021'","handlingStrategy":"validation","validationCode":"function parseDateSafe(s) {\n  if (typeof s !== 'string' || !/^(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})$/.test(s)) {\n    throw new Error(\"Expected dd/mm/yyyy\")\n  }\n  return parseDate(s)\n}","typeGuard":"const isDdMmYyyy = (s) => typeof s === 'string' && /^(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})$/.test(s)","tryCatchPattern":"try { parseDate(str) } catch (e) { if (/Invalid date format/.test(e.message)) { /* normalize format then retry */ } else throw e }","preventionTips":["Normalize ISO dates to dd/mm/yyyy before calling: split on '-' and reorder.","Replace dashes/dots with slashes: s.replace(/[-.]/g, '/').","Ensure four-digit years; pad two-digit years upstream."],"tags":["date","validation","format","regex","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}