Today =
today = datetime.datetime.now().strftime("%Y%m%d")
90days ago
DD = datetime.timedelta(days=-90)
How do I get today - DD, or 90days ago?
For adding or subtracting Date, we use something called timedelta() function which can be found under the DateTime class. It is used to manipulate Date, and we can perform arithmetic operations on dates like adding or subtracting.
You can subtract a day from a python date using the timedelta object. You need to create a timedelta object with the amount of time you want to subtract. Then subtract it from the date.
You can use simple date arithmetic to find the number of days between two dates in Python. Define the 2 dates between which you want to find the difference in days. Then subtract these dates to get a timedelta object and examine the day's property of this object to get the required result.
The timedelta function allows you to perform date addition and date subtraction calculations that can add or subtract specified numbers of days from a date and return a new date before or after the original date.
You should be able to subtract time deltas from datetime objects.
today = datetime.datetime.now() DD = datetime.timedelta(days=90) earlier = today - DD earlier_str = earlier.strftime("%Y%m%d")
Just subtract a timedelta from a datetime:
>>> import datetime >>> today = datetime.datetime.today() >>> DD = datetime.timedelta(days=90) >>> today - DD datetime.datetime(2010, 11, 3, 9, 56, 20, 924224)
(or if you want to use a negative timedelta like you did there, add them:
>>> DD = datetime.timedelta(days=-90) >>> today + DD datetime.datetime(2010, 11, 3, 9, 56, 20, 924224)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With