How to Write a Date in YAML
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
<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 aT.
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
opened: 2026-03-14
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:
import sys
from pprint import pp
import yaml
with open(sys.argv[1], encoding="utf-8") as file:
pp(yaml.safe_load(file))
uv run python show.py dates.yaml
{'opened': datetime.date(2026, 3, 14),
'inspected': 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 says | What PyYAML returned |
|---|---|
opened: "2026-03-14", in quotes | The text '2026-03-14' |
opened: 2026-3-14, with a one-digit month | The text '2026-3-14' |
opened: 2026-02-30, a day that doesn't exist | An error: ValueError: day 30 must be in range 1..28 for month 2 in year 2026 |
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
One-digit months or days: 2026-3-14 | It stays text, without an error | Write two digits: 2026-03-14 |
| Expecting text and getting a date | Code that treats the value as a string fails | Quote it if you want text: "2026-03-14" |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
ValueError: day ... must be in range while loading the file | A date that doesn't exist on the calendar | Correct the date |
| A date arrives as text | Quotes around it, or a one-digit month or day | Remove 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
- YAML 1.1 timestamp type — every date and time form PyYAML reads.
- datetime.date — what you can do with the loaded value.
