fromordinal():

fromordinal : Return date by using Proleptic Gregorian ordinal .

About ordinal by using toordinal()

Today's fromordinal vlaue

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

Example 1: Convert a Past Ordinal to Date

from datetime import date  

past_ordinal = 730000  
print(date.fromordinal(past_ordinal))  # Output: 1999-03-05  

Example 2: Convert Future Ordinal to Date

future_ordinal = 800000  
print(date.fromordinal(future_ordinal))  # Output: 2191-04-29 

Example 3: Convert Today's Date to Ordinal and Back

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 

Example 4: Handling Very Large Ordinals

large_ordinal = 1000000  
print(date.fromordinal(large_ordinal))  # Output: 2738-11-28 

Example 5: Converting Year 1, Day 1

first_ordinal = 1  
print(date.fromordinal(first_ordinal))  # Output: 0001-01-01  

Handling Invalid Ordinal Values

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

Calculating the Number of Days Between Two Dates

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}")

Converting Between Ordinal and Timestamp

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}")

All Date Objects toordinal()
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer