Python datetime.day() Method

Musfirah Waseem Jan 30, 2023
  1. Syntax of Python datetime.day() Method
  2. Example Codes: Use datetime.day() Method in Python
  3. Example Codes: Use datetime.day() Method to Find the Current Day
  4. Example Codes: Enter an Invalid Date in the datetime.day() Method
Python datetime.day() Method

Python datetime.day() method is an efficient way of finding the day in the Gregorian calendar format.

Syntax of Python datetime.day() Method

datetime.day(year, month, day)

Parameters

year The year should be between 0 and 9999.
month The month should be between 1 and 12.
day The number of days in the specified month.

Return

The return type of this class is an object of Datetime instance. The date is returned in the right format.

Example Codes: Use datetime.day() Method in Python

import datetime

date = datetime.date(2019, 4, 13)

print(date)

Output:

2019-04-13

This method adds leading zeros in decimal integer literals.

Example Codes: Use datetime.day() Method to Find the Current Day

import datetime

datetime_object = datetime.date.today()

print(datetime_object)

Output:

2020-09-03

This function datetime.day() is used to make more class methods, like the one given in the above code

Example Codes: Enter an Invalid Date in the datetime.day() Method

import datetime

date = datetime.date(20000, 1, 9)

print(date)

Output:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    date = datetime.date(20000, 1, 9)
ValueError: year 20000 is out of range

If an argument of the date is outside the specified ranges, then a ValueError or OSError exception is raised.

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