Reputation: 19
Currently I am making the Query to find out to get the data from SQL server.
SELECT * FROM ExcelOutput WHERE ADateTime >= '2014-12-03 07:00:00' AND ADateTime < '2014-12-04 12:00:00'
I need to create one method to get two Date object as input. The second one will be the DateTime.Now() as the input data and the first ADateTime will be the Yesterday one, how I can write the command as Yesterday? DateTime.Now .... somethings to minus one day.
Edited
What I want is
function(Today 7 a.m., Yesterday 7 a.m.) {
}
in C#.
Then I can connect with that SQL to retrieve the data.
This is the Correct Answer.
Try this snippet,
DateTime today = DateTime.Now.Date;
today = today.AddHours(7);
DateTime yesterday = today.Subtract(TimeSpan.FromHours(24));
SELECT * FROM ExcelOutput WHERE ADateTime >= 'yesterday' AND ADateTime < 'today'
Credit to: Manu Nair
Upvotes: 1
Views: 3215
Reputation: 19
This is the Correct Answer.
Try this snippet,
DateTime today = DateTime.Now.Date;
today = today.AddHours(7);
DateTime yesterday = today.Subtract(TimeSpan.FromHours(24));
SELECT * FROM ExcelOutput WHERE ADateTime >= 'yesterday' AND ADateTime < 'today'
Credit to: Manu Nair
Upvotes: 0
Reputation: 322
Try this snippet,
DateTime today = DateTime.Now.Date;
today = today.AddHours(7);
DateTime yesterday = today.Subtract(TimeSpan.FromHours(24));
SELECT * FROM ExcelOutput WHERE ADateTime >= 'yesterday' AND ADateTime < 'today'
Upvotes: 1
Reputation: 2020
You can subtract one day from DateTime.Now
by
DateTime yest = DateTime.Now.AddDays(-1);
You can also do this to any DateTime
object.
If you want to subtract one day from your custom date, create an object of DateTime
by
example
DateTime myVar = new DateTime(2014, 6, 14, 6, 32, 0);
myVar.AddDays(-1);
If you want to create a specific time,
DateTime reqTime = new DateTime(yest.Year,yest.Month,yest.Day,myCustomhour,myCustomminute,myCustomsecond);
Upvotes: 0
Reputation: 101192
Yesterday at 13:00:
var yesterday = DataTime.Today.AddDays(-1).AddHours(13);
Today gives date only.
Upvotes: 0
Reputation: 39105
How to get the specific time of today (and yesterday):
var today = DateTime.Now.Date;
var todayAtSeven = DateTime.Now.Date.Add(TimeSpan.FromHours(7));
var yesterdayAtSeven = today.AddDays(-1).Add(TimeSpan.FromHours(7));
Upvotes: 1
Reputation: 575
I think this is possible to do with DateTime.Now.AddDays
:
DateTime tomorrow = DateTime.Now.AddDays(1); //tomorow
DateTime yesterday = DateTime.Now.AddDays(-1); //yesterday
Upvotes: 0