Serdia
Serdia

Reputation: 4418

How to get month name from date in python

the goal is to get month name and year from previous month date. Such: October -2019

But I got an error:

import datetime


d = datetime.date.today()
first_day = d.replace(day=1) # gives first day of current month
last_day = first_day - datetime.timedelta(days=1) # gives last date of previous month
print(last_day.strtime("%B"))

Error:

Traceback (most recent call last):
  File "x:\Documents\Python\tempCodeRunnerFile.py", line 7, in <module>
    print(last_day.strtime("%B"))
AttributeError: 'datetime.date' object has no attribute 'strtime'

Upvotes: 1

Views: 5371

Answers (1)

JacobIRR
JacobIRR

Reputation: 8946

The proper method to use is called strftime, not strtime

Upvotes: 4

Related Questions