Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare dates with four digit years

Tags:

python

date

I need to compare dates in this format:

'03/31/2018' # month/day/year

I tried to use the datetime module:

from datetime import datetime as dt
dt.strptime("03/31/2018", "%m/%d/%y")

But I got this error:

ValueError: unconverted data remains: 18

If I use a two digit year:

dt.strptime("03/31/18", "%m/%d/%y")

It works, but in this case I need to compare using the whole four digits year

like image 891
Luis Ramon Ramirez Rodriguez Avatar asked Dec 18 '22 17:12

Luis Ramon Ramirez Rodriguez


1 Answers

You need to use a big Y

>>> from datetime import datetime as dt
>>> dt.strptime("03/31/2018", "%m/%d/%Y")
datetime.datetime(2018, 3, 31, 0, 0)
like image 131
Noel Evans Avatar answered Dec 21 '22 10:12

Noel Evans