Fromordinal() Function Of Datetime.date Class In Python
The fromordinal() function is used to return the Gregorian date corresponding to a specified Gregorian ordinal. This is the opposite of the toordinal() function that is used to convert a Gregorian date to a Gregorian ordinal. When a negative ordinal value or an ordinal beyond the value returned by the date.max.toordinal() is passed to the parameter of the toordinal() function, this function raises a ValueError.
Syntax: @classmethod fromordinal(ordinal)
Parameters: This function accepts a parameter which is illustrated below:
- ordinal: This is the specified Gregorian ordinal for which the Gregorian date is going to be found.
Return values: This function returns the Gregorian date corresponding to a specified Gregorian ordinal.
Example 1: From the specific day of the Gregorian calendar.
# Python3 code for getting
# the Gregorian date corresponding
# to a given Gregorian ordinal.
# Importing datetime module
import datetime
# Specifying a Gregorian ordinal
ordinal = 123456;
# Calling the fromordinal() function
# over the specified Gregorian ordinal
date = datetime.date.fromordinal(ordinal);
# Printing the Gregorian date
print("The Gregorian date for the Gregorian\
ordinal %d is: %s"%(ordinal, date));
Output:
The Gregorian date for the Gregorian ordinal 123456 is: 0339-01-05
Example 2: From first day of the Gregorian calendar.
# Python3 code for getting
# the Gregorian date corresponding
# to a given Gregorian ordinal.
# Importing datetime module
import datetime
# Calling the fromordinal() function over
# the 1st day of Gregorian calendar as its parameter
date = datetime.date.fromordinal(1);
# Printing the Gregorian date for the 1st date
# of Gregorian calendar
print("Gregorian Date for the 1st day \
of Gregorian calendar: %s"%date);
Output:
Gregorian Date for the 1st day of Gregorian calendar: 0001-01-01