Get Day of Month - 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.

Examples

1. Get day of month from current date

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

Python Program

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

Output

2019-06-26 08:40:13.638249
26

2. Get day of month from given date

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

Python Program

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

Output

2019-08-12 00:00:00
12

Summary

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