c# - 如何在C#中获取昨天的日期

标签 c# asp.net

我想使用 C# 在我的 ASP.NET Web 应用程序中检索昨天的日期。 我曾尝试寻找解决方案,但收效甚微。我使用的代码只输出今天的日期:

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

如何获取昨天的日期?

最佳答案

使用DateTime.AddDays()值为 -1

的方法
var yesterday = DateTime.Today.AddDays(-1);

这会给你:{6/28/2012 12:00:00 AM}

你也可以使用

DateTime.Now.AddDays(-1)

这将为您提供当前时间的上一个日期,例如{6/28/2012 上午 10:30:32}

关于c# - 如何在C#中获取昨天的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11256459/

相关文章:

ASP.NET MVC Controller参数处理

c# - 将 C# 属性读入 JQuery 代码

html - 根据条件隐藏 ascx 标记

c# - 如何在我需要时打开门

c# - 我在这里需要抽象类或接口(interface)吗(或者两者都不需要)?

c# - 如何从 ConcurrentBag<> 中删除单个特定对象?

javascript - 我的启动项目中的 JS 文件又出现 'The Breakpoint will not currently be hit' 错误

asp.net - 确定 IIS 7 使用什么帐户来访问文件夹(和其他资源)

c# - 互斥 : is this safe?

c# - Entity Framework 忽略 NotMapped 属性