python - Separate one year from a data value; taking care of leap years -
this should pretty simple can't syntax correct. so, have parameter dictionary below -
paramdict = { startpostingdatefilter": { "msgprompt": "start date", "datatype": "datetime", "tablefield": [{"table":"tablename", "field":"columnname"}], "value": ["2006-01-01"] } } startpostingdatefilter = paramdict['startpriordatefilter']['value'][0]
now, want subtract 1 year "startpriordatefilter" user provided date value. how take care of if it's leap year? want achieve below -
if, startpostingdatefilter = '2006-01-01' create new variable, newstartpostingdatefilter = '2005-01-01'
thanks.
you use dateutil:
from dateutil.relativedelta import relativedelta dateutil import parser d = parser.parse(startpostingdatefilter) print((d - relativedelta(years=1)).date()) 2005-01-01
you use datetime , replace catching feb 29th returning year - 1 , feb 28th if was:
d = datetime.strptime(startpostingdatefilter, "%y-%m-%d") sub = d.replace(year=d.year - 1) if (d.month != 2 , d.day) != 29 else datetime(d.year-1, 2, 28)
Comments
Post a Comment