I am parsing some datetime strings in Python 2.7, using datetime.strptime
. I want to assume that a date is prior to now.
But strptime\'s %y operator do
If your input is in the local timezone:
from datetime import date
then = datetime.strptime('10/12/68', '%d/%m/%y').date()
if date.today() <= then: # *then* must be in the past
then = then.replace(year=then.year - 100)
It should work ok until 2100 (excluding). See here for more details on arithmetic with calendar years.
It's easy to fix after the fact:
from datetime import datetime, timedelta
dt = datetime.strptime(...)
if dt > datetime.now():
dt -= timedelta(years=100)