Jacky
Jacky

Reputation: 97

datetime.strptime is giving me the wrong month

I have to use datetime to change the a date from str to date but I'm getting an unexpected result:

strd = "2020-02-06"
dated = datetime.strptime(strd, '%Y-%M-%d').date()
print(dated)

I expect the result should be 2020-02-06 but the output is 2020-01-06.

What is causing this?

Upvotes: 1

Views: 698

Answers (1)

Chris
Chris

Reputation: 137268

%M is for minutes, not months. Since you aren't providing a value for months you're getting the default.

Use %m instead:

dated = datetime.strptime(strd, '%Y-%m-%d').date()
#                                    ^

Upvotes: 3

Related Questions