date.fromordinal(ordinal) returns the date for a proleptic Gregorian ordinal: a day count where 1 is 0001-01-01 and each later day adds one. Day 738886 is 2024-01-01.
It is the exact inverse of toordinal(). datetime.fromordinal() does the same and returns a datetime at midnight, with tzinfo set to None.
date.fromordinal() is a classmethod of datetime.date: call it on the class itself, as date.fromordinal(...), and it returns a new object. Import the class first, for example from datetime import date. The datetime class has a method with the same name.
Syntax
Pass a whole number of days.
python
day = date.fromordinal(ordinal)
moment = datetime.fromordinal(ordinal)
Call
What it does
date.fromordinal(1)
Returns datetime.date(1, 1, 1): the first supported day.
An int from 1 (0001-01-01) to 3652059 (9999-12-31).
Return Value
Return Value
Description
date
The matching calendar day (from date.fromordinal).
datetime
The same day at 00:00:00, naive (from datetime.fromordinal).
Python Examples
Example 1 — Turn Day Numbers into Dates
Convert a few known ordinals and the day after 28 February in a leap year.
python
from datetime import date
print(date.fromordinal(1))
print(date.fromordinal(738886))
print(date.fromordinal(719163))
print(date.fromordinal(date(2024, 2, 28).toordinal() + 1))
Output
0001-01-01
2024-01-01
1970-01-01
2024-02-29
How It Works
Day one. Ordinal 1 is the first day of year 1, printed with four year digits.
Recent dates. 738886 is 1 January 2024, and 719163 is the Unix epoch date.
Leap day included. Adding 1 to the ordinal of 28 February 2024 lands on 29 February because 2024 is a leap year.
Example 2 — Store a Schedule as Day Numbers
Keep dates as plain ints, add day offsets, and convert back to dates and to a midnight datetime.
python
from datetime import date, datetime
start = date(2024, 3, 15).toordinal()
schedule = [start + offset for offset in (0, 14, 28)]
print(schedule)
print([date.fromordinal(n).isoformat() for n in schedule])
print(datetime.fromordinal(schedule[-1]))
Ints are easy to store. A day number fits in any integer column and sorts naturally.
Plain addition. Adding 14 and 28 to the start ordinal gives the fortnightly dates.
Back to dates.date.fromordinal() plus isoformat() produces readable strings.
Midnight datetime. The datetime version sets the time to 00:00:00.
Example 3 — Check the Valid Ordinal Range
Find the largest ordinal and see which values raise ValueError.
python
from datetime import date
print(date.max.toordinal())
for n in (0, 3652059, 3652060):
try:
print(n, date.fromordinal(n))
except ValueError:
print(n, "is out of range")
Output
3652059
0 is out of range
3652059 9999-12-31
3652060 is out of range
How It Works
Upper limit.date.max is 9999-12-31, and its ordinal is 3652059.
No day zero. Counting starts at 1, so 0 raises ValueError.
Past the end. One more than the maximum would be in year 10000, which is not supported.
Frequently Asked Questions
A day count that applies the Gregorian calendar backwards to year 1. Day 1 is 0001-01-01.
toordinal(). date.fromordinal(d.toordinal()) == d is always True.
No. It always returns midnight, and tzinfo is None.
The valid range starts at 1. Values below 1 or above 3652059 raise ValueError.
Not directly, because Excel counts from a different start. Add a fixed offset, or add timedelta(days=n) to Excel's base date.