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

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -