How to get Day of Month using Python datetime?

Python datetime – Get Day of Month

To get Weekday Full Version using Python datetime, call strftime() with %d passed as argument. The day of month range from 01 to 31.

Example 1: Get Day of Month of Current Date

In the following example, we will take the current date, and get the day of month.

#import datetime package
import datetime

#get current time
d = datetime.datetime.now()

#print date
print(d)

#get the day of month
print(d.strftime("%d"))
Run this program ONLINE

Output

2019-06-26 08:40:13.638249
26

Example 2: Get Day of Month of Specific Date

In the following example, we will take a date 2019-08-12 and get the day of month.

#import datetime package
import datetime

#set a date
d = datetime.datetime(2019, 8, 12)

#print date
print(d)

#get the day of month
print(d.strftime("%d"))
Run this program ONLINE

Output

2019-08-12 00:00:00
12

Summary

In this tutorial, we extracted the day of month from date using datetime package.