要显示前一天,请使用AddDays()
方法和值-1来获取前一天。
首先,使用以下命令获取当前日期-
DateTime.Today
现在,将-1添加到AddDays()
方法中以获取前一天-
DateTime.Today.AddDays(-1)
以下是代码-
using System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { Console.WriteLine("Today = {0}", DateTime.Today); Console.WriteLine("Previous Day = {0}", DateTime.Today.AddDays(-1)); } }
输出结果
Today = 9/4/2018 12:00:00 AM Previous Day = 9/3/2018 12:00:00 AM