Parsing Dates and Times Correctly
Avoid day-month mix-ups, convert to ISO 8601, handle time zones and spreadsheet serial dates, and catch impossible dates before they skew results.
Welcome back!
Step 1 of 12Learn
Is 03/04/2025 in March or April?
A US analyst merged sales data from a London office with data from Chicago. The combined monthly chart showed a strange pattern: London sales spiked in the first 12 days of every month and disappeared for the rest.
The London system wrote dates as day/month/year, so 03/04/2025 meant 3 April. The analyst's tool assumed month/day/year and read it as March 4. Every date with a day of 12 or less was parsed into the wrong month. Dates with a day of 13 or more, such as 25/04/2025, could not be read as month/day at all, so they became missing, which explains the missing second half of each month.
Dates are one of the richest sources of cleaning bugs because there are so many conventions: 03/04/2025, 4/3/25, 2025-04-03, 3 Apr 2025, April 3rd, 20250403. Times add time zones, daylight saving and 12- versus 24-hour clocks.
The rule that prevents most problems: never let a tool guess a date format when you can tell it. Parse with an explicit format per source, then store dates in one unambiguous standard. The rest of this lesson shows how to choose that standard, detect the format of an unknown file and handle the special cases.
Step 2 of 12Compare
One string, two dates
| Aspect | Read month-first (US) | Read day-first (UK and most of the world) |
|---|---|---|
| 03/04/2025 | March 4, 2025 | 3 April 2025 |
| 12/01/2025 | December 1, 2025 | 12 January 2025 |
| 25/04/2025 | Invalid (no month 25), becomes missing | 25 April 2025 |
| 2025-04-03 (ISO 8601) | April 3, 2025 | April 3, 2025 |
Step 3 of 12Learn
ISO 8601 and detecting formats
The international standard ISO 8601 writes dates as YYYY-MM-DD, for example 2025-04-03, and timestamps as 2025-04-03T14:30:00Z, where Z means UTC. It has three big advantages: nobody can confuse day and month, text sorting equals date sorting, and almost every tool reads it correctly. Use it for cleaned data and file names.
To parse an unknown slash-separated column, gather evidence:
- If any value has a first field above 12, such as 25/04/2025, the format is day-first.
- If any value has a second field above 12, such as 04/25/2025, it is month-first.
- If both fields are 12 or less in every row, the column is ambiguous. Look at the source system's country, documentation or known dates, like a holiday sale you know happened on 25 November.
Never decide row by row. A column comes from one system with one format; if a parser "guesses" per value, ambiguous rows will be read one way and unambiguous ones the other, creating exactly the London bug.
After parsing, run range checks: no order dates before the company existed, no future birth dates, no shipping dates before order dates. These catch both format mistakes and typos like 2052 for 2025.
Step 4 of 12Practice · Choose one
A date column from an unknown system contains 07/11/2024, 02/05/2024, 13/09/2024 and 01/12/2024. What can you conclude?
Step 5 of 12Practice · Code challenge · Python
Write to_iso(text, day_first) that converts a date written as three numbers separated by "/", "-" or "." (for example "03/04/2025" or "7.1.2024", year last) into an ISO string "YYYY-MM-DD". If day_first is True the first number is the day, otherwise the month. Return None for impossible dates such as 31 February. Use datetime.date to validate.
to_iso("03/04/2025", True)to_iso("03/04/2025", False)to_iso("7.1.2024", True)to_iso("31-02-2025", True)
Step 6 of 12Learn
Time zones and daylight saving
A timestamp without a time zone is incomplete. "2025-03-14 23:30" in New York (daylight time, UTC−4) is already 03:30 on March 15 in UTC. If one system logs local time and another logs UTC, merging them shifts events by hours, and near midnight, by a whole day. Daily totals, "orders per day" and time-to-ship calculations all go wrong.
Practical rules:
- Store in UTC, display in local time. UTC has no daylight saving jumps, so durations and orderings are always correct.
- Record the zone of the source. When you receive local times, find out which zone they are in and convert explicitly. A column named
created_attells you nothing;created_at_utctells you everything. - Watch the daylight saving switches. In the US, clocks jump from 2:00 to 3:00 on the spring change day, so 2:30 local time doesn't exist that day; in the fall, 1:30 happens twice. Event counts per hour will show a gap or a double spike that is not real behavior.
- Decide what "a day" means. Is a sale at 23:30 Pacific on the 31st part of this month or next month? For business reports, the answer is usually the company's local time zone, chosen on purpose.
Offsets also change during the year: New York is UTC−5 in winter and UTC−4 in summer, so never hard-code one offset for a whole year.
Step 7 of 12Practice · Work it out
A support ticket was opened at 21:15 on January 10 in New York, which is UTC−5 in winter. At what hour (0–23) on the UTC clock was it opened?
Step 8 of 12Learn
Spreadsheet serial dates and two-digit years
Two more date traps show up constantly in real files.
Serial dates. Spreadsheets store dates as numbers: days counted from a starting point. In Excel's default system, serial 1 is January 1, 1900, so 45000 is March 15, 2023 and 45658 is January 1, 2025. When a spreadsheet is exported carelessly, a date column can arrive as a column of five-digit numbers. Convert with the right starting point; in Python, adding the number of days to December 30, 1899 matches modern Excel dates, because Excel counts a nonexistent February 29, 1900. Some older Mac spreadsheets used a 1904 starting point, which shifts everything by 1,462 days, so check a known date.
Two-digit years. "01/02/68" could be 1968 or 2068. Tools use a pivot year to decide; Python's %y reads 69–99 as the 1900s and 00–68 as the 2000s. For birth dates that is clearly wrong: someone born in "68" is almost certainly not born in 2068. Apply a domain rule, such as "a birth year cannot be in the future", and fix the century explicitly.
Both problems produce values that are valid dates, just wrong ones. Range checks and a few manually verified rows are your defense.
Step 9 of 12Note
Step 10 of 12Practice · Sort into groups
Sort these date values: already valid ISO 8601 dates, or values that need fixing.
Tap an item, then tap its group (or drag it there).
Step 11 of 12Practice · True or false
Storing timestamps in UTC and converting to local time only for display avoids most daylight saving and merging errors.
Step 12 of 12Practice · Choose one
A customer table has birth dates like "04/17/52" parsed with a default two-digit-year rule, giving 2052-04-17 for some customers. What is the best fix?
Lesson complete!