AP257 AP257 - 2 years ago 139
Python Question

Python: get datetime for '3 years ago today'?

This must be a duplicate (and documented), but I can't find the answer via Google.

In Python, how do I get a datetime object for '3 years ago today'?


UPDATE: FWIW, I don't care hugely about accuracy... i.e. it's Feb 29th today, I don't care whether I'm given Feb 28th or March 1st in my answer. Concision is more important than configurability, in this case.

Answer Source
import datetime
datetime.datetime.now() - datetime.timedelta(days=3*365)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download