ADVERTISEMENT
ADVERTISEMENT

Python datetime toordinal() Method with Example

Python datetime.toordinal() Method: Here, we are going to learn about the toordinal() method of datetime class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 02, 2020

Python datetime.toordinal() Method

datetime.toordinal() method is used to manipulate objects of datetime class of module datetime.

It is used to return 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:

    import datetime

Class:

    from datetime import datetime

Syntax:

    toordinal()

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:

## 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


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.