toordinal() Function Of Datetime.date Class In Python
The toordinal() function is used to return the proleptic Gregorian ordinal of a specified datetime instance.
Note: The Proleptic Gregorian ordinal gives the number of days elapsed from the date 01/Jan/0001. And here ordinal is called Proleptic since the Gregorian calendar itself is followed from October 1582.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Syntax: toordinal()
Parameters: This function does not accept any parameter.
Return values: This function returns the proleptic Gregorian ordinal of a datetime instance.
Example 1: Using today’s date.
Python3
# Python3 code to demonstrate# Getting the proleptic Gregorian# ordinal of a datetime instance # importing datetime and time moduleimport datetimeimport time # Getting today's datetodays_Date = datetime.date.fromtimestamp(time.time()); # Calling the toordinal() function over the# today's datedate = todays_Date.toordinal(); # Printing the proleptic Gregorian ordinal# for the today's dateprint("Proleptic Ordinal for today's date: %s"%date); |
Output:
Proleptic Ordinal for today's date: 737998
Example 2: Using today’s date and time.
Python3
# Python3 code to demonstrate# Getting the proleptic Gregorian# ordinal of a datetime instance # importing datetime and time moduleimport datetimeimport time # Getting today's date and timetodays_DateTime = datetime.datetime.now(); # Calling the toordinal() function over the# today's date and timeDateTime = todays_DateTime.toordinal(); # Printing the proleptic Gregorian ordinal# for the today's date and timeprint("Proleptic Ordinal for today's date and time: %s"%DateTime); |
Output:
Proleptic Ordinal for today's date and time: 737998
Example 3: Using a specific date.
Python3
# Python3 code to demonstrate# Getting the proleptic Gregorian# ordinal of a datetime instance # importing datetime and time moduleimport datetimeimport time # Initializing a date and timeDateTime = datetime.datetime(1358, 8, 12, 1, 3, 4, 9); # Calling the toordinal() function over the# above date and timeDate_Time = DateTime.toordinal(); # Printing the proleptic Gregorian ordinal# for the above given date and timeprint("Proleptic Ordinal for today's date and time: %s"%Date_Time); |
Output:
Proleptic Ordinal for today's date and time: 495858


