Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> now.replace(year=now.year-1)

Yeah but this is bad code. Python certainly does have a "clean" way to subtract a year, you subtract a datetime.timedelta object.



timedelta doesn't take "years" as a parameter, for the reasons others have listed here. It's ambiguous what subtracting by a year means, and there's no real sensible default either.


relativedelta will do it:

  from dateutil.relativedelta import relativedelta
  one_year = relativedelta(years=1)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: