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.

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"))
Run Code Copy

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"))
Run Code Copy

Output

2019-08-12 00:00:00
12

Summary

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

Related Tutorials

Code copied to clipboard successfully 👍