Python datetime.toordinal() Method

Python datetime.toordinal() Method

  1. Syntax of Python datetime.toordinal() Method
  2. Example 1: Use the datetime.toordinal() Method in Python
  3. Example 2: Enter Out-Of-Range Date as a Parameter to datetime.toordinal() Method
  4. Example 3: Find the Day Number Using the datetime.toordinal() Method
  5. Example 4: Maximum and Minimum Values of the datetime.toordinal() Method

Python datetime.toordinal() method is an efficient way of finding the proleptic Gregorian ordinal of the date. Note that January 1 of year 1 has ordinal 1 and so on.

Syntax of Python datetime.toordinal() Method

datetime.toordinal()

Parameters

This method doesn’t accept any parameter.

Return

This method returns an object representing the ordinal value for the specified datetime object.

Example 1: Use the datetime.toordinal() Method in Python

import datetime

datetime_object = datetime.date.today()

toordinal = datetime_object.toordinal()

print(f"The ordinal of the date {datetime_object} is {toordinal}.")

Output:

The ordinal of the date 2022-08-31 is 738398.

The above code displays the Gregorian ordinal for today’s datetime object.

Example 2: Enter Out-Of-Range Date as a Parameter to datetime.toordinal() Method

import datetime

date = datetime.datetime(18, 0, 0)

toordinal = date.toordinal()

print(f"The ordinal value of the earliest Datetime {date} is {toordinal}")

Output:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    date = datetime.datetime(18, 0, 0)
ValueError: month must be in 1..12

The date should be in the given range; otherwise, a ValueError exception is raised.

Example 3: Find the Day Number Using the datetime.toordinal() Method

import datetime

date = datetime.date(1776, 7, 4)

print("America got its independence on {}".format(date))

toordinal = date.toordinal()

print("America got its independence on {}th day if you count from 01/01/01".format(toordinal))

Output:

America got its independence on 1776-07-04
America got its independence on 648491th day if you count from 01/01/01

Like the above code, you can mention any specific date you want to find the day number.

Example 4: Maximum and Minimum Values of the datetime.toordinal() Method

import datetime

date = datetime.datetime(1, 1, 1, 0,0,0,0)

print("The ordinal number of the earliest possible date allowed",date," is: ", date.toordinal())

date = datetime.datetime(9999, 12, 31, 23, 59, 59)

print("The ordinal number of the largest maximum date allowed",date," is: ", date.toordinal())

Output:

The ordinal number of the earliest possible date allowed 0001-01-01 00:00:00  is:  1
The ordinal number of the largest maximum date allowed 9999-12-31 23:59:59  is:  3652059

The Gregorian calendar is only defined until 9999, and a year number greater than this is not allowed.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python DateTime

  • Python datetime.datetime.year Attribute
  • Python datetime.tzinfo Class
  • Python datetime.datetime.replace() Method
  • Python datetime.timezone Class
  • Python Datetime.fromtimestamp() Method
  • Python Datetime.timedelta() Method