1

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'
2
  • 2
    The error message is correct. It appears you are looking for strftime() [note the "f"!]. The %B should be the correct format code. Commented Nov 20, 2019 at 23:03
  • 2
    Ooooh! Attention to details!!! Thank you ! Commented Nov 20, 2019 at 23:05

1 Answer 1

4

The proper method to use is called strftime, not strtime

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.