from datetime import date
my_ordinal = date.today().toordinal()
print("Today toordinal value:", my_ordinal)
print("Today date:", date.fromordinal(my_ordinal))
Output
Today toordinal value : 737319
Today date : 2019-09-17
Using today and tomorrow ordinal vlaue
from datetime import date, datetime, timedelta
my_today = date.today().toordinal()
print("Today toordinal output:", my_today)
my_tomorrow = date.today() + timedelta(days=1)
my_tomorrow = my_tomorrow.toordinal()
print("Tomorrow toordinal output:", my_tomorrow)
print(date.fromordinal(my_today))
print(date.fromordinal(my_tomorrow))
Output
Today toordinal : 737319
Tomorrow toordinal : 737320
2019-09-17
2019-09-18
from datetime import date
past_ordinal = 730000
print(date.fromordinal(past_ordinal)) # Output: 1999-03-05
future_ordinal = 800000
print(date.fromordinal(future_ordinal)) # Output: 2191-04-29
from datetime import date
today = date.today()
ordinal_value = today.toordinal() # Convert today's date to ordinal
print(date.fromordinal(ordinal_value)) # Output: Current date
large_ordinal = 1000000
print(date.fromordinal(large_ordinal)) # Output: 2738-11-28
first_ordinal = 1
print(date.fromordinal(first_ordinal)) # Output: 0001-01-01
It's important to note that providing an invalid ordinal value to date.fromordinal() will raise a ValueError. An ordinal is considered invalid if it's less than 1 or greater than the maximum ordinal value supported by Python's date object.
from datetime import date
invalid_ordinal = 0 # Ordinal less than 1
try:
result = date.fromordinal(invalid_ordinal)
except ValueError as e:
print(f"Error: {e}")
invalid_ordinal = date.max.toordinal() + 1 # Ordinal greater than max supported value
try:
result = date.fromordinal(invalid_ordinal)
except ValueError as e:
print(f"Error: {e}")
Output
Error: ordinal must be >= 1
Error: year 10000 is out of range
By converting dates to their ordinal values, we can easily calculate the number of days between them.
from datetime import date
date1 = date(2025, 2, 8)
date2 = date(2025, 3, 10)
days_between = date2.toordinal() - date1.toordinal()
print(f"Number of days between {date1} and {date2}: {days_between}")
While date.fromordinal() deals with ordinal values, timestamps are another way to represent dates. We can convert between these representations using the datetime module.
from datetime import datetime, date
ordinal_value = 738000
dt = datetime.fromordinal(ordinal_value)
timestamp = dt.timestamp()
print(f"Ordinal: {ordinal_value}")
print(f"DateTime: {dt}")
print(f"Timestamp: {timestamp}")
# Converting back
dt_from_timestamp = datetime.fromtimestamp(timestamp)
ordinal_from_timestamp = dt_from_timestamp.toordinal()
print(f"DateTime from Timestamp: {dt_from_timestamp}")
print(f"Ordinal from Timestamp: {ordinal_from_timestamp}")
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.