How to subtract a day from a date?
You can use a timedelta
object:
from datetime import datetime, timedelta
d = datetime.today() - timedelta(days=days_to_subtract)
Subtract days from a DateTime
That error usually occurs when you try to subtract an interval from DateTime.MinValue
or you want to add something to DateTime.MaxValue
(or you try to instantiate a date outside this min-max interval). Are you sure you're not assigning MinValue
somewhere?
Subtract one day from datetime
Try this
SELECT DATEDIFF(DAY, DATEADD(day, -1, '2013-03-13 00:00:00.000'), GETDATE())
OR
SELECT DATEDIFF(DAY, DATEADD(day, -1, @CreatedDate), GETDATE())
Python datetime minus 1 day using timedelta
Some well placed parentheses should suffice:
(datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y-%m-%d')
A friendly suggestion: take a look at Python string formatting instead of constructing your string using concatenation. It'll end up being a lot cleaner and less repetitive.
How to subtract days from a plain Date?
Try something like this:
var d = new Date();
d.setDate(d.getDate()-5);
Note that this modifies the date object and returns the time value of the updated date.
var d = new Date();
document.write('Today is: ' + d.toLocaleString());
d.setDate(d.getDate() - 5);
document.write('<br>5 days ago was: ' + d.toLocaleString());
Subtract one day from DateTime object
Just use the AddDays
method, remembering that it doesn't change the value it's called on - it returns a new DateTime
value.
DateTime date = ...;
date = date.AddDays(-1);
subtract one day from a pandas dataframe date column
You can use .dt.hour == 0
with np.where()
to identify rows where the hour is 0 and - pd.tseries.offsets.Day()
to subtract a day.
df['date'] = pd.to_datetime(df['date'])
df['FinalStartPunch'] = pd.to_datetime(df['FinalStartPunch'], format='%d%b%Y:%H:%M:%S')
df['Date1'] = df['FinalStartPunch'].where((df['FinalStartPunch'].dt.hour != 0),
df['date'] - pd.tseries.offsets.Day())
df
Out[37]:
date FinalStartPunch Date1
0 2015-06-27 2015-06-27 14:15:00 2015-06-27 14:15:00
1 2015-07-23 2015-07-23 13:31:00 2015-07-23 13:31:00
2 2015-07-23 2015-07-23 18:43:00 2015-07-23 18:43:00
3 2015-08-15 2015-08-15 18:35:00 2015-08-15 18:35:00
4 2015-08-15 2015-08-15 23:30:00 2015-08-15 23:30:00
5 2015-08-16 2015-08-16 00:00:00 2015-08-15 00:00:00
6 2016-01-30 2016-01-30 18:25:00 2016-01-30 18:25:00
7 2016-01-30 2016-01-30 23:52:00 2016-01-30 23:52:00
8 2016-01-31 2016-01-31 00:00:00 2016-01-30 00:00:00
9 2016-08-13 2016-08-13 18:30:00 2016-08-13 18:30:00
10 2016-08-13 2016-08-13 23:58:00 2016-08-13 23:58:00
11 2016-08-14 2016-08-14 00:00:00 2016-08-13 00:00:00
12 2017-01-28 2017-01-28 18:30:00 2017-01-28 18:30:00
13 2017-01-28 2017-01-28 23:57:00 2017-01-28 23:57:00
14 2017-01-29 2017-01-29 00:00:00 2017-01-28 00:00:00
I have a list of dates and I want to subtract actual date from each of them to know how many day passed. Is there any fast way to do this?
If I'm understanding correctly, you need to find the current date, and then find the number of days between the current date and the dates in your list?
If so, you could try this:
from datetime import datetime, date
dates = ['2019-10-11', '2013-05-16', '2011-06-16', '2000-04-22']
actual_date = date.today()
days = []
for date in dates:
date_object = datetime.strptime(date, '%Y-%m-%d').date()
days_difference = (actual_date - date_object).days
days.append(days_difference)
print(days)
What I am doing here is:
- Converting the individual date strings to a "date" object
- Subtracting the this date from the actual date. This gets you the time as well, so to strip that out we add .days.
- Save the outcome to a list, although of course you could do whatever you wanted with the output.
Related Topics
Rails: Get Next/Previous Record
Differencebetween Rowsbetween and Rangebetween
When Should You Use Full-Text Indexing
Change Column Types in a Huge Table
Truncate Multiple Tables in One MySQL Statement
Insert into Table Without Specifying Column Names
Omitting the Milliseconds in a Date
Is Bigint(8) the Largest Integer MySQL Can Store
How to Convert Int to Date in SQL Server 2008
How to Pass a Comma Separated List to a Stored Procedure
Oracle: SQL Query That Returns Rows with Only Numeric Values
MySQL Select Query String Matching
How to Compare Data Between Two Table in Different Databases Using SQL Server 2008
Count Rows Per Hour in SQL Server with Full Date-Time Value as Result
Delphi - Prevent Against SQL Injection
The Version of SQL Server in Use Does Not Support Datatype Datetime2