×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python datetime toordinal() Method with Example

Python datetime.toordinal() Method: In this tutorial, we will learn about the toordinal() method of datetime class in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 22, 2023

Python datetime.toordinal() Method

The datetime.toordinal() method is used to get the proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1. If January 1 of year 1 has ordinal number 1 then January 2 year 1 will have ordinal number 2 and so on. It is an instance method which means that it works on an instance of the class.

Module

The following module is required to use toordinal() method:

import datetime

Class

The following class is required to use toordinal() method:

from datetime import datetime

Syntax

The following is the syntax of toordinal() method:

toordinal()

Parameter(s)

The following are the parameter(s):

  • None

Return Value

The return type of this method is a number which is the ordinal number of that date in Gregorian calendar.

Example of datetime toordinal() Method in Python

## importing datetime class
from datetime import datetime

## Creating an instance
x = datetime(2020,10,27, 21, 5,27, 100)
d = x.toordinal()
print("Ordinal number of date ",x," is:", d)
print()

## Using today' date
x = datetime.now()
d = x.toordinal()
print("Ordinal number of date ",x," is:", d)
print()

x = datetime.today()
d = x.toordinal()
print("Ordinal number of date ",x," is:", d)
print()

x = datetime(1, 1, 1, 0,0,0,0)
d = x.toordinal()
print("Ordinal number of the earliest possible date allowed",x," is:", d)
print()

x = datetime(9999, 12, 31, 23, 59, 59)
d = x.toordinal()
print("Ordinal number of the largest maximum date allowed",x," is:", d)
print()

Output

Ordinal number of date  2020-10-27 21:05:27.000100  is: 737725

Ordinal number of date  2020-05-02 06:50:06.336005  is: 737547

Ordinal number of date  2020-05-02 06:50:06.336068  is: 737547

Ordinal number of the earliest possible date allowed 0001-01-01 00:00:00  is: 1

Ordinal number of the largest maximum date allowed 9999-12-31 23:59:59  is: 3652059

Note: Attribute of the datetime objects should be in the given range otherwise it will show a ValueError.

Example:

## importing datetime class
from datetime import datetime

## Creating an instance
x = datetime(99999, 1, 31)
d = x.toordinal()
print("Ordinal number of the date",x," is:", d)
print()

Output

Traceback (most recent call last):
  File "main.py", line 5, in <module>
    x = datetime(99999, 1, 31)
ValueError: year 99999 is out of range

Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.