How to Create a Date in Python with datetime.date
A date holds a calendar day with no time: a year, a month and a day. Python's datetime.date builds one from three numbers or from text such as 2026-03-31, and you can compare and subtract the results. This guide does all of it, with real output from Python 3.15.2.
Syntax
from datetime import date
<name> = date(<year>, <month>, <day>)
<name> = date.fromisoformat("<YYYY-MM-DD>")
| Part | What it means |
|---|---|
<year>, <month>, <day> | Whole numbers. The month runs from 1 to 12, and the day has to exist in that month |
"<YYYY-MM-DD>" | The same date as text, in ISO 8601 form, such as "2026-03-31" |
<name>.year, <name>.month, <name>.day | The three parts of a date, as numbers |
The datetime reference says of date(year, month, day): "All arguments are required. Arguments must be integers", and "If an argument outside those ranges is given, ValueError is raised."
The Example
A short script makes two dates, the first of January and the last of March 2026. It prints the first one, then the second one's parts, checks which one comes later and works out how far apart they are.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
from datetime import date
start = date(2026, 1, 1)
end = date.fromisoformat("2026-03-31")
print(start)
print(end.year, end.month, end.day)
print(end > start)
print(end - start)

Step 2: Run It
uv run python dates.py
2026-01-01
2026 3 31
True
89 days, 0:00:00

print(start)shows the date as2026-01-01, in ISO 8601 form..year,.monthand.dayare plain numbers:2026 3 31. The month is3, not03.end > startcompares dates by when they fall. The end of March comes after New Year, so it printsTrue.end - startgives a length of time, atimedelta:89 days, 0:00:00.
The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
A month past 12: date(2026, 13, 1) | ValueError: month must be in 1..12, not 13 | Use a month from 1 to 12 |
A day the month doesn't have: date(2026, 2, 30) | ValueError: day 30 must be in range 1..28 for month 2 in year 2026 | Use a day that exists in that month and year |
Leading zeros: date(2026, 01, 05) | SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers | Write the numbers without zeros in front: date(2026, 1, 5) |
Comparing a date with text: date(2026, 3, 31) > "2026-01-01" | TypeError: '>' not supported between instances of 'datetime.date' and 'str' | Turn the text into a date first, with date.fromisoformat("2026-01-01") |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
ValueError that names a day and a month | The day doesn't exist in that month, such as 30 February | Check the day against the month and year; February 2026 has 28 days |
SyntaxError about leading zeros | A number such as 05 was typed with a zero in front | Remove the zero |
FAQ
What is the difference between date and datetime?
A date holds a day only. The datetime reference describes a datetime as "A combination of a date and a time", so it adds hours, minutes and seconds.
How do I get today's date?
With date.today(). The reference says it returns "the current local date", so it depends on your computer's time zone.
Conclusion
date(2026, 1, 1) and date.fromisoformat("2026-03-31") built two dates. Their parts came back as plain numbers, > compared them, and subtracting one from the other gave 89 days, 0:00:00. Numbers that don't make a real date raise a ValueError that names the problem.
Additional Resources
- date objects — every constructor, attribute and method of
date. - date.fromisoformat — the text formats it accepts.
