month from a date in python

You can extract the month from a date in Python by using the strftime function from the datetime module. Here's an example:

main.py
from datetime import datetime

date_string = "2021-04-15"
date_object = datetime.strptime(date_string, "%Y-%m-%d")
month = date_object.strftime("%B")

print(month)  # Output: April
181 chars
8 lines

In the code above, we first convert the date string "2021-04-15" to a datetime object using the strptime method. We then use the strftime method to format the datetime object into a string representing the month, which is extracted using the %B format code.

The output of the code above is April, which is the month of the date "2021-04-15".

gistlibby LogSnag