Date/time format: recognizing dates and times in a text string

How date/time formats work and how to find, parse and normalize dates and timestamps in plain text without altering the original content.

A date/time format is the agreed layout of a calendar date, a clock time, or both, such as 2026-10-31T14:05:00Z or 10/31/2026 2:05 PM. When dates sit inside ordinary text, like log lines, emails, invoices or notes, you first have to recognize them, then read them correctly. This guide explains what date and time extraction does, which formats you are likely to meet, and how to pull timestamps out of a string step by step, then check that nothing was misread or changed.

What date and time extraction does for your text

Purpose: extraction locates every date, time or combined timestamp in a piece of text and turns each one into a value a program can compare, sort or calculate with.

Input: any plain text. That can be a single string, a file, or many lines of mixed content in which dates appear in one or more formats, sometimes alongside numbers that only look like dates, such as version numbers, prices or phone numbers.

Output: a list of matches. Each match ideally has three parts: the exact original substring, its position in the text, and a parsed, normalized value, usually written in ISO 8601, for example 2026-03-04T14:15:00.

Scope: extraction is a read-only operation. The source text should stay exactly as it was. If you also want to rewrite dates in place, for example converting every US date to ISO 8601, treat that as a separate replacement step that runs only after you have checked the extracted matches.

Common date and time stamp formats

The examples below all describe the same moment, 2:05 PM UTC on October 31, 2026, except where noted. They are illustrations of each layout, not output from a specific tool.

Format | Example | What to know
ISO 8601 date | 2026-10-31 | Year, month, day; sorts correctly as plain text
ISO 8601 date and time, UTC | 2026-10-31T14:05:00Z | T separates date and time; Z means UTC
ISO 8601 with offset | 2026-10-31T09:05:00-05:00 | A fixed offset from UTC, not a named time zone
RFC 3339 style with space | 2026-10-31 14:05:00Z | RFC 3339 allows a space in place of T for readability
US numeric | 10/31/2026 2:05 PM | Month first, 12-hour clock; local time unless stated
Day-first numeric | 31/10/2026 14:05 | Common outside the US; clashes with US order when the day is 12 or less
Written month | Oct 31, 2026 or October 31, 2026 | Month names depend on language
Email/HTTP style | Sat, 31 Oct 2026 14:05:00 +0000 | Weekday, day, month name, year, time, offset
Unix timestamp, seconds | 1793455500 | Seconds since 1970-01-01T00:00:00Z; 10 digits for current dates
Unix timestamp, milliseconds | 1793455500000 | Same moment in milliseconds; 13 digits

Format codes: how a datetime format is described

Parsing libraries need to be told the layout using format codes, also called pattern tokens. The table shows the codes used by C and Python strftime/strptime. Other systems use different tokens: Java, .NET and Unicode LDML patterns write yyyy-MM-dd HH:mm, where MM is month and mm is minute, so capitalization changes the meaning.

Code | Example | Meaning
%Y | 2026 | Four-digit year
%y | 26 | Two-digit year
%m | 10 | Month, 01–12
%B | October | Full month name (locale-dependent)
%b | Oct | Abbreviated month name (locale-dependent)
%d | 31 | Day of month, 01–31
%H | 14 | Hour, 00–23
%I | 02 | Hour, 01–12
%M | 05 | Minute
%S | 00 | Second
%p | PM | AM/PM marker (locale-dependent)
%z | -0500 | UTC offset

For example, the string 10/31/2026 2:05 PM corresponds to the format %m/%d/%Y %I:%M %p, and 2026-10-31 14:05:00 corresponds to %Y-%m-%d %H:%M:%S.

How to recognize dates and times in a string, step by step

  • 1. Collect real samples. Look through representative text and list every date and time layout that actually appears, including odd ones such as dates without years or times without seconds.
  • 2. Settle ambiguous conventions. For a US audience, 03/04/2026 normally means March 4, but in day-first regions it means 3 April. Confirm with the text's source rather than guessing.
  • 3. Write one pattern per format. A regular expression (a compact search pattern for text) can find candidates. Put the most specific patterns first, so a full date-and-time match is taken before the date alone.
  • 4. Capture the exact span. Keep the matched substring and its start and end positions unchanged, so you can trace every result back to the source.
  • 5. Parse each match with an explicit format. Do not rely on automatic guessing; give the parser the exact format code string for that pattern.
  • 6. Reject impossible values. A pattern like \d{2}/\d{2}/\d{4} also matches 02/30/2026, which is not a real date. A strict parser raises an error for it; treat that as a non-match.
  • 7. Normalize the output. Convert each parsed value to one format, typically ISO 8601, and store it next to the original text rather than replacing it.
  • 8. Verify the result using the checks further below.

Example patterns

These regular expressions are illustrative starting points. They find candidates only; each match still needs to be parsed and validated.

ISO 8601 date with optional time and offset:
\b\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:?\d{2})?)?

US numeric date, month first:
\b(0?[1-9]|1[0-2])/(0?[1-9]|[12]\d|3[01])/(\d{4}|\d{2})\b

12-hour time with AM/PM:
\b(1[0-2]|0?[1-9]):[0-5]\d(?::[0-5]\d)? ?[AaPp][Mm]\b

The short Python sketch below shows the full flow: find, parse with an explicit format, normalize. It is an example and has not been run for this article. Note that datetime.fromisoformat accepts a trailing Z only in Python 3.11 and later.

import re
from datetime import datetime

text = "Invoice sent 03/04/2026 at 2:15 PM; paid 2026-03-09T10:30:00-05:00."

us_pattern = re.compile(r"\b(\d{1,2}/\d{1,2}/\d{4}) at (\d{1,2}:\d{2} [AP]M)\b")
iso_pattern = re.compile(r"\b\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:Z|[+-]\d{2}:\d{2})")

for m in us_pattern.finditer(text):
    value = datetime.strptime(f"{m.group(1)} {m.group(2)}", "%m/%d/%Y %I:%M %p")
    print(m.span(), m.group(0), "->", value.isoformat())

for m in iso_pattern.finditer(text):
    value = datetime.fromisoformat(m.group(0))
    print(m.span(), m.group(0), "->", value.isoformat())

If it behaves as intended, the first match should normalize to 2026-03-04T14:15:00 (no time zone, because the source gave none) and the second should keep its offset, 2026-03-09T10:30:00-05:00. Run it on your own data before relying on it.

Pitfalls that cause misread dates

  • Month/day order: 03/04/2026 is valid in both conventions and gives different dates; only context can settle it.
  • Two-digit years: 03/04/26 forces the parser to choose a century; check which rule your library applies.
  • 12 AM and 12 PM: 12:00 AM is midnight and 12:00 PM is noon. Parsing with %H instead of %I gets these wrong.
  • Missing time zone: a time with no Z or offset is local to whoever wrote it. Record it as unknown rather than assuming UTC.
  • Offsets are not time zones: -05:00 says nothing about daylight saving rules; a named zone such as America/New_York does.
  • False positives: version numbers (1.2.2026), ranges (10-12), IDs and phone numbers can match loose patterns. Word boundaries and validation help.
  • Unix timestamps: a 10-digit number may be seconds, a 13-digit number milliseconds, or neither. Only treat numbers as timestamps when the field or context says so.
  • Locale-dependent names: Oct, Okt and oct. are all abbreviations for October in different languages or styles, and parsing month names or AM/PM markers depends on the active locale.

Invisible and Unicode characters

Text that looks identical can contain different characters, and a pattern that expects a normal space will silently skip a timestamp that uses another one. Common cases:

  • No-break space (U+00A0) or narrow no-break space (U+202F) between the time and AM/PM, as in 2:05[U+202F]PM. Some locale data and software produce the narrow form.
  • En dash (U+2013) or minus sign (U+2212) instead of a hyphen (U+002D), as in 2026–10–31 or an offset like −05:00.
  • Full-width or other non-ASCII digits, such as 2026. In Python 3, \d in a text pattern matches these too, which may or may not be what you want; use [0-9] to accept ASCII digits only.
  • Zero-width characters, such as zero-width space (U+200B), hidden inside copied text.

Decide deliberately whether to accept these variants. If you normalize them, for example replacing U+202F with a regular space, do it on a copy used for matching and keep positions mapped back to the original, so the source text is not altered.

How to verify the result

  • Count check: compare the number of matches with a manual count on a sample of lines.
  • Round trip: format each parsed value back into the source layout and confirm it equals the original substring.
  • Spot-check edge cases: dates with day 12 or less, midnight and noon, end-of-month dates, leap days such as 2028-02-29, and lines with two dates.
  • Search for misses: look for leftover digit groups with slashes, dashes or colons that were not matched, and inspect them.
  • Confirm the source is unchanged: compare the original text before and after processing; a pure extraction step should leave it byte-for-byte identical.

Gaps in this guide

No evidence about a specific extraction tool, product or library version was available for this article, so it does not compare tools, list their supported formats or report performance. The formats, format codes and patterns above are general conventions and illustrations; check the documentation of whatever tool or language you use, and test on your own text, before treating any output as correct.