Two order exports can contain the same value, 03/04/2026, and mean different things. A day-first export means 3 April. A month-first export means 4 March. Both are valid calendar dates, so trying one parser and falling back to the other only works when the first parser fails. It cannot resolve this case.
Choose the format from the source
Get the date format from the supplier's export specification or your own import configuration. The example below assumes one known exporter consistently uses DD/MM/YYYY and another uses MM/DD/YYYY. An export's country label by itself is not a format guarantee.
import re
from datetime import datetime
DATE_FORMATS = {"uk_export": "%d/%m/%Y", "us_export": "%m/%d/%Y"}
DATE_SHAPE = re.compile(r"[0-9]{2}/[0-9]{2}/[0-9]{4}")
def order_date(raw, source):
if source not in DATE_FORMATS:
raise ValueError("unknown source date format")
if not DATE_SHAPE.fullmatch(raw):
raise ValueError("expected two digits, slash, two digits, slash, four digits")
return datetime.strptime(raw, DATE_FORMATS[source]).date().isoformat()
print(order_date("03/04/2026", "uk_export"))
print(order_date("03/04/2026", "us_export"))
Output with Python 3.12:
2026-04-03
2026-03-04
The regular expression enforces the shape promised by these exporters. Python's strptime() checks the calendar date and accepts an optional leading zero for day and month on its own. For this strict contract, 3/4/2026 is rejected by the shape check; 31/04/2026 passes that check but is rejected by the date parser.
Keep rows you cannot interpret out of the report
Record the row identifier, original date and reason in an exception report rather than silently changing formats. Ask the supplier to confirm the format, or request YYYY-MM-DD for future date-only exports. Treat timestamps with times and offsets as a separate input contract.
References: Python format-code documentation. For general background, see Howcodex’s Python 3 date and time guide. This post is shared on behalf of Howcodex.