zeval
zeval

Reputation: 187

Wrong datetime information

I have the string "5-11-2019" in DAY/MONTH/YEAR format, and am doing the following to increment 1 day:

datetime.datetime.strptime(str("5-11-2019"), '%d-%M-%Y') + datetime.timedelta(days=1)

However, instead of getting the result of 2019-11-06, Python returns 2019-01-06, removing 10 months, which I cannot for the life of me understand.

Upvotes: 0

Views: 184

Answers (3)

Joe - Check out my books
Joe - Check out my books

Reputation: 16943

Check the “%M”.

Also keep in mind that

%d = Day of the month as a zero-padded decimal number. and %-d = Day of the month as a decimal number. (Platform specific) [From https://strftime.org/]

So either pad your days with 0 or use “%-d”.

Upvotes: 0

difurious
difurious

Reputation: 1542

You should be using %m instead of %M

%m = Months
%M = Minutes

Upvotes: 1

RobinFrcd
RobinFrcd

Reputation: 5466

You used %d-%M-%Y as format, but %M stands for minute, not month. You should use %d-%m-%Y (lower m).

Upvotes: 1

Related Questions