如何在C#中获取昨天的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11256459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get yesterday's date in C#
提问by akhil
I want to retrieve yesterday's date in my ASP.NET web application using C#.
我想使用 C# 在我的 ASP.NET Web 应用程序中检索昨天的日期。
I already digged about the topic but I guess I m not able to understand it.
我已经深入研究了这个话题,但我想我无法理解它。
Code i m using is just giving me todays date
我使用的代码只是给了我今天的日期
string yr = DateTime.Today.Year.ToString();
string mn = DateTime.Today.Month.ToString();
string dt = DateTime.Today.Day.ToString();
date = string.Format("{0}-{1}-{2}", yr, mn, dt);
How can I do so?
我怎么能这样做?
Thanks in Advance :)
提前致谢 :)
采纳答案by Habib
Use DateTime.AddDays()method with value of -1
使用DateTime.AddDays()方法,其值为-1
var yesterday = DateTime.Today.AddDays(-1);
That will give you : {6/28/2012 12:00:00 AM}
那会给你: {6/28/2012 12:00:00 AM}
You can also use
你也可以使用
DateTime.Now.AddDays(-1)
That will give you previous date with the current time e.g. {6/28/2012 10:30:32 AM}
这将为您提供当前时间的上一个日期,例如 {6/28/2012 10:30:32 AM}
回答by moribvndvs
var yesterday = DateTime.Now.AddDays(-1);
回答by Chris Moutray
Something like this should work
这样的事情应该工作
var yesterday = DateTime.Now.Date.AddDays(-1);
DateTime.Nowgives you the currentdate and time.
DateTime.Now为您提供当前日期和时间。
If your looking to remove the the time element then adding .Dateconstrains it to the date only ie time is 00:00:00.
如果您希望删除时间元素,则添加.Date仅将其限制为日期,即时间为00:00:00.
Finally .AddDays(-1)removes 1 day to give you yesterday.
.AddDays(-1)昨天终于去掉1天给你。
回答by Hitesh Patel
You will get yesterday date by this following code snippet.
您将通过以下代码片段获得昨天的日期。
DateTime dtYesterday = DateTime.Now.Date.AddDays(-1);
回答by Mark Byers
The code you posted is wrong.
您发布的代码是错误的。
You shouldn't make multiple calls to DateTime.Today. If you happen to run that code just as the date changes you could get completely wrong results. For example if you ran it on December 31st 2011 you might get "2011-1-1".
您不应多次调用DateTime.Today. 如果您恰好在日期更改时运行该代码,则可能会得到完全错误的结果。例如,如果您在 2011 年 12 月 31 日运行它,您可能会得到“2011-1-1”。
Use a single call to DateTime.Todaythen use ToStringwith an appropriate format string to format the date as you desire.
使用单个调用DateTime.Today然后使用ToString适当的格式字符串来根据需要格式化日期。
string result = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd");
回答by kavinhuh
string result = DateTime.Now.Date.AddDays(-1).ToString("yyyy-MM-dd");
回答by V4Vendetta
DateTime.Todayas it implies is todays date and you need to get the Date a day before so you subtract one day using AddDays(-1);
DateTime.Today正如它所暗示的那样是今天的日期,您需要在前一天获取日期,因此您可以使用AddDays(-1);
There are sufficient options available in DateTime to get the formatting like ToShortDateStringdepending on your culture and you have no need to concatenate them individually.
DateTime 中有足够的选项可以ToShortDateString根据您的文化获得格式,您无需单独连接它们。
Also you can have a desirable format in the .ToString()version of the DateTimeinstance
您也可以.ToString()在DateTime实例版本中使用所需的格式
回答by Talha
You don't need to call DateTime.Todaymultiple times, just use it single time and format the date object in your desire format.. like that
您不需要DateTime.Today多次调用,只需一次使用它并以您想要的格式格式化日期对象..就像那样
string result = DateTime.Now.Date.AddDays(-1).ToString("yyyy-MM-dd");
OR
或者
string result = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd");
回答by NIshank
DateTime dateTime = DateTime.Now ;
string today = dateTime.DayOfWeek.ToString();
string yesterday = dateTime.AddDays(-1).DayOfWeek.ToString(); //Fetch day i.e. Mon, Tues
string result = dateTime.AddDays(-1).ToString("yyyy-MM-dd");
The above snippet will work. It is also advisable to make single instance of DateTime.Now;
上面的代码段将起作用。还建议制作 DateTime.Now 的单个实例;

