Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subtract a day from a date?

I have a Python datetime.datetime object. What is the best way to subtract one day?

like image 981
defrex Avatar asked Jan 13 '09 22:01

defrex


People also ask

How do you subtract day?

Therefore, you can add or subtract days as easy as adding or minus the number of days in Excel. 1. Select a blank cell you will place the calculating result, type the formula =A2+10, and press the Enter key. Note: For subtracting 10 days from the date, please use this formula =A2–10.

How do you subtract days from new date?

You can use d. setDate(d. getDate() + days) with both positive and negative values for days to add and subtract days respectively. And it should work on the instance (as other Date methods do), not create and return a copy.

How do I subtract a day from a date in SQL?

Following the answer from Philip Rego, you can use SELECT GETDATE() - 1 to subtract days from a date.


2 Answers

You can use a timedelta object:

from datetime import datetime, timedelta      d = datetime.today() - timedelta(days=days_to_subtract) 
like image 140
Steve B. Avatar answered Oct 03 '22 14:10

Steve B.


Subtract datetime.timedelta(days=1)

like image 36
S.Lott Avatar answered Oct 03 '22 14:10

S.Lott