Fromordinal() Function Of Datetime.date Class In Python
Last Updated :
29 Aug, 2021
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
import datetime
ordinal = 123456 ;
date = datetime.date.fromordinal(ordinal);
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
import datetime
date = datetime.date.fromordinal( 1 );
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...