Skip to main content

How to Create a Date in Python with datetime.date

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
from datetime import date

<name> = date(<year>, <month>, <day>)
<name> = date.fromisoformat("<YYYY-MM-DD>")
PartWhat 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>.dayThe 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​

dates.py
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)

The dates.py script in a macOS-style code window: it imports date from datetime, builds start with date(2026, 1, 1) and end with date.fromisoformat("2026-03-31"), then prints start, the parts of end, end > start and end - start.

Step 2: Run It​

Terminal
uv run python dates.py
Output
2026-01-01
2026 3 31
True
89 days, 0:00:00

The terminal after uv run python dates.py: 2026-01-01, then 2026 3 31, then True, then 89 days, 0:00:00.

  • print(start) shows the date as 2026-01-01, in ISO 8601 form.
  • .year, .month and .day are plain numbers: 2026 3 31. The month is 3, not 03.
  • end > start compares dates by when they fall. The end of March comes after New Year, so it prints True.
  • end - start gives a length of time, a timedelta: 89 days, 0:00:00.

The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
A month past 12: date(2026, 13, 1)ValueError: month must be in 1..12, not 13Use 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 2026Use 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 integersWrite 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​

SymptomLikely causeFix
ValueError that names a day and a monthThe day doesn't exist in that month, such as 30 FebruaryCheck the day against the month and year; February 2026 has 28 days
SyntaxError about leading zerosA number such as 05 was typed with a zero in frontRemove 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​