Skip to main content

How to Write a Date in YAML

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A date written as 2026-03-14 in a YAML file isn't just text: PyYAML loads it as a real Python date, ready to compare and calculate with. Add a time and it becomes a date and time. This guide writes both, checks them with Python, and shows the forms that don't become dates.

Syntax​

Syntax
<key>: <YYYY>-<MM>-<DD>
<key>: <YYYY>-<MM>-<DD> <hh>:<mm>:<ss>
  • <YYYY>-<MM>-<DD>: a four-digit year, a two-digit month and a two-digit day.
  • <hh>:<mm>:<ss>: an optional time, after a space or a T.

The YAML 1.1 timestamp type, which PyYAML follows, describes "A point in time" that "can be serialized using a subset of the ISO8601 format". Its date form is [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].

The Example​

dates.yaml records when a bike shop opened, and the date and time it was last inspected.

Prerequisites​

  • uv and Python, with PyYAML in a project (uv add pyyaml), to check the file. This guide uses PyYAML 6.0.4 on Python 3.15.2.

Step 1: Write the File​

dates.yaml
opened: 2026-03-14
inspected: 2026-03-14 09:30:00

dates.yaml in a macOS-style code window: opened: 2026-03-14 and inspected: 2026-03-14 09:30:00.

Step 2: Check It with PyYAML​

Save this checker as show.py. It loads any YAML file you give it and prints the result:

show.py
import sys
from pprint import pp

import yaml

with open(sys.argv[1], encoding="utf-8") as file:
pp(yaml.safe_load(file))
Terminal
uv run python show.py dates.yaml
Output
{'opened': datetime.date(2026, 3, 14),
'inspected': datetime.datetime(2026, 3, 14, 9, 30)}

The terminal after uv run python show.py dates.yaml: opened as datetime.date(2026, 3, 14) and inspected as datetime.datetime(2026, 3, 14, 9, 30).

opened became a date, and inspected, with its time, a datetime. Neither is text any more. The command works unchanged in PowerShell.

Forms That Don't Become Dates​

What the file saysWhat PyYAML returned
opened: "2026-03-14", in quotesThe text '2026-03-14'
opened: 2026-3-14, with a one-digit monthThe text '2026-3-14'
opened: 2026-02-30, a day that doesn't existAn error: ValueError: day 30 must be in range 1..28 for month 2 in year 2026

Common Mistakes​

MistakeWhat happensDo this instead
One-digit months or days: 2026-3-14It stays text, without an errorWrite two digits: 2026-03-14
Expecting text and getting a dateCode that treats the value as a string failsQuote it if you want text: "2026-03-14"

Troubleshooting​

SymptomLikely causeFix
ValueError: day ... must be in range while loading the fileA date that doesn't exist on the calendarCorrect the date
A date arrives as textQuotes around it, or a one-digit month or dayRemove the quotes, or write two digits

FAQ​

How do I write a time zone?​

Add it to the end. 2026-03-14T09:30:00Z loaded as datetime.datetime(2026, 3, 14, 9, 30, tzinfo=datetime.timezone.utc), a time in UTC.

Is a YAML date always a Python date?​

In PyYAML, yes, when it's written as YYYY-MM-DD without quotes. Other tools may load it as text, so check how your program reads the file.

Conclusion​

opened: 2026-03-14 loaded as a Python date, and with a time it became a datetime. Write two-digit months and days, quote dates you want as text, and expect a ValueError for a day that doesn't exist.

Additional Resources​