You can extract the month from a date in Python by using the strftime
function from the datetime
module. Here's an example:
main.py181 chars8 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