TheAlgorithms/JavaScript · error · Error

Invalid date format. Please use 'dd/mm/yyyy'.

Error message

Invalid date format. Please use 'dd/mm/yyyy'.

What it means

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.

Source

Thrown at Timing-Functions/ParseDate.js:15

import { getMonthDays } from './GetMonthDays'

function checkDate(date) {
  if (date.day < 1 || date.day > getMonthDays(date.month, date.year)) {
    throw new Error('Invalid day value.')
  }
}

function parseDate(dateString) {
  const regex = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/

  const match = dateString.match(regex)

  if (!match) {
    throw new Error("Invalid date format. Please use 'dd/mm/yyyy'.")
  }

  const res = {
    day: parseInt(match[1], 10),
    month: parseInt(match[2], 10),
    year: parseInt(match[3], 10)
  }
  checkDate(res)
  return res
}

export { parseDate }

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Format input as dd/mm/yyyy with slashes and a four-digit year: parseDate('15/01/2021').
  2. If your source is ISO, reformat before calling: const [y,m,d] = iso.split('-'); parseDate(`${d}/${m}/${y}`).
  3. Convert other separators to slashes: s.replace(/[-.]/g, '/').
  4. Pad two-digit years and ensure month/day are 1–2 digits before calling.

Example fix

// before
parseDate('2021-01-15') // ISO, throws

// after
const [y, m, d] = '2021-01-15'.split('-')
parseDate(`${d}/${m}/${y}`) // '15/01/2021'
Defensive patterns

Strategy: validation

Validate before calling

function parseDateSafe(s) {
  if (typeof s !== 'string' || !/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/.test(s)) {
    throw new Error("Expected dd/mm/yyyy")
  }
  return parseDate(s)
}

Type guard

const isDdMmYyyy = (s) => typeof s === 'string' && /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/.test(s)

Try / catch

try { parseDate(str) } catch (e) { if (/Invalid date format/.test(e.message)) { /* normalize format then retry */ } else throw e }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/d643da3ef7d292d9. Report an issue: GitHub.