How to Use the Datetime toordinal() Method in Python

09/18/2021

Contents

In this article, you will learn how to use the Datetime toordinal() method in Python.

Datetime toordinal() Method

The toordinal() method is a part of the Python datetime module and is used to convert a datetime object into a proleptic Gregorian ordinal. The proleptic Gregorian calendar extends the Gregorian calendar backwards to dates preceding its introduction in 1582.

Here’s how you can use the toordinal() method in Python:

from datetime import datetime

# create a datetime object
dt = datetime(2022, 3, 1)

# convert datetime object to proleptic Gregorian ordinal
ordinal = dt.toordinal()

print(ordinal) # Output: 737033

In the example above, we first create a datetime object with the date March 1st, 2022. We then call the toordinal() method on the dt object to convert it into a proleptic Gregorian ordinal. Finally, we print out the ordinal value, which is 737033.

Note that the toordinal() method returns an integer value representing the number of days between January 1, year 1 and the specified date, where January 1 of year 1 has ordinal value 1.