简体   繁体   English

C#DateTime.Now查找昨天

[英]C# DateTime.Now to find yesterday

Currently I am making the Query to find out to get the data from SQL server. 当前,我正在查询以查找从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. 我需要创建一种方法来获取两个Date对象作为输入。 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()作为输入数据,第一个ADateTime将是昨天的输入数据,我如何将命令编写为昨天? DateTime.Now .... somethings to minus one day. DateTime.Now ....大约要减去一天。

Edited 编辑

What I want is 我想要的是

function(Today 7 am, Yesterday 7 am) { 功能(今天上午7点,昨天上午7点){

} }

in C#. 在C#中。

Then I can connect with that SQL to retrieve the data. 然后,我可以与该SQL连接以检索数据。

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 归功于: Manu Nair

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'

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));

You can subtract one day from DateTime.Now by 您可以从DateTime.Now减去一天。

DateTime yest = DateTime.Now.AddDays(-1);

You can also do this to any DateTime object. 您也可以对任何DateTime对象执行此操作。 If you want to subtract one day from your custom date, create an object of DateTime by 如果要从自定义日期减去一天,请通过以下方式创建DateTime对象

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);

I think this is possible to do with DateTime.Now.AddDays : 我认为这可能与DateTime.Now.AddDays

DateTime tomorrow = DateTime.Now.AddDays(1); //tomorow
DateTime yesterday = DateTime.Now.AddDays(-1); //yesterday

Yesterday at 13:00: 昨天13:00:

var yesterday = DataTime.Today.AddDays(-1).AddHours(13);

Today gives date only. 今天只给出日期。

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 归功于: Manu Nair

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM