计算两个日期之间的差异(天数)?

我看到这个问题已经为Javajavascriptphp回答了,但不是C#。那么,如何在C#中计算两个日期之间的天数呢?

1532928 次浏览

假设StartDateEndDate是类型DateTime

(EndDate - StartDate).TotalDays

使用TimeSpan对象,它是日期继承的结果:

DateTime d1;DateTime d2;return (d1 - d2).TotalDays;

我认为这会做你想要的:

DateTime d1 = DateTime.Now;DateTime d2 = DateTime.Now.AddDays(-1);
TimeSpan t = d1 - d2;double NrOfDays = t.TotalDays;
DateTime xmas = new DateTime(2009, 12, 25);double daysUntilChristmas = xmas.Subtract(DateTime.Today).TotalDays;
protected void Calendar1_SelectionChanged(object sender, EventArgs e){DateTime d = Calendar1.SelectedDate;// int a;TextBox2.Text = d.ToShortDateString();string s = Convert.ToDateTime(TextBox2.Text).ToShortDateString();string s1 =  Convert.ToDateTime(Label7.Text).ToShortDateString();DateTime dt = Convert.ToDateTime(s).Date;DateTime dt1 = Convert.ToDateTime(s1).Date;if (dt <= dt1){Response.Write("<script>alert(' Not a valid Date to extend warranty')</script>");}else{string diff = dt.Subtract(dt1).ToString();Response.Write(diff);Label18.Text = diff;Session["diff"] = Label18.Text;}}

如果有人想要整个天数作为双精度(ab类型DateTime):

 (a.Date - b.Date).TotalDays

对于ab作为两个DateTime类型:

DateTime d = DateTime.Now;DateTime c = DateTime.Now;c = d.AddDays(145);string cc;Console.WriteLine(d);Console.WriteLine(c);var t = (c - d).Days;Console.WriteLine(t);cc = Console.ReadLine();

先声明一个稍后会返回的类:

public void date(){Datetime startdate;Datetime enddate;Timespan remaindate;
startdate = DateTime.Parse(txtstartdate.Text).Date;enddate = DateTime.Parse(txtenddate.Text).Date;
remaindate = enddate - startdate;
if (remaindate != null){lblmsg.Text = "you have left with " + remaindate.TotalDays + "days.";}else{lblmsg.Text = "correct your code again.";}}
protected void btncal_Click(object sender, EventArgs e){date();}

使用按钮控件调用上面的类。这是一个例子:

你可以试试这个

EndDate.Date.Subtract(DateTime.Now.Date).Days

最上面的答案是正确的,但是如果你只想要整数天,并且很乐意放弃日期的时间部分,那么考虑:

(EndDate.Date - StartDate.Date).Days

再次假设StartDateEndDate是类型DateTime

获取两个日期之间的差异,然后从以下位置获取天数:

int total_days = (EndDate - StartDate).TotalDays
// Difference in days, hours, and minutes.
TimeSpan ts = EndDate - StartDate;
// Difference in days.
int differenceInDays = ts.Days; // This is in intdouble differenceInDays= ts.TotalDays; // This is in double
// Difference in Hours.int differenceInHours = ts.Hours; // This is in intdouble differenceInHours= ts.TotalHours; // This is in double
// Difference in Minutes.int differenceInMinutes = ts.Minutes; // This is in intdouble differenceInMinutes= ts.TotalMinutes; // This is in double

您还可以获得秒,毫秒和滴答的差异。

您可以使用下面的代码:

 int DateDifInSecond = EndDate.Subtract(StartDate).TotalSeconds

使用时间跨度可以解决问题,因为它有许多属性:

DateTime strt_date = DateTime.Now;DateTime end_date = Convert.ToDateTime("10/1/2017 23:59:59");//DateTime add_days = end_date.AddDays(1);TimeSpan nod = (end_date - strt_date);Console.WriteLine(strt_date + "" + end_date + "" + "" + nod.TotalHours + "");Console.ReadKey();

对于像我这样的初学者,会偶然发现这个小问题,在一个简单的行中,样本转换为int

int totalDays = Convert.ToInt32((DateTime.UtcNow.Date - myDateTime.Date).TotalDays);

这将计算从今天(DateTime. UtcNow. Date)到所需日期(myDateTime. Date)的总天数。

如果myDateTime是昨天,或比今天更早的日期,这将给出一个正(+)整数结果。

另一方面,如果myDateTime是明天或未来的日期,由于加法规则,这将给出负(-)整数结果。

编码愉快!^_^

在计算两个日期之间的天数时,经常有关于时间(小时)的争论。对这个问题的回答和他们的评论也不例外。

考虑到StartDateEndDate是类型DateTime:如果性能不是问题,我强烈建议通过中间转换记录您的计算。例如,(EndDate - StartDate).Days是不直观的,因为舍入将取决于StartDateEndDate的小时分量。

  • 如果您希望以天为单位的持续时间包括天的分数,那么正如已经建议的那样使用(EndDate - StartDate).TotalDays.
  • 如果您希望持续时间反映两天之间的距离,则使用(EndDate.Date - StartDate.Date).Days
  • 如果您希望持续时间反映开始日期的早上和晚上之间的持续时间结束日期(您通常在项目管理软件中看到的),然后使用(EndDate.Date - StartDate.Date).Days + 1

试试这个真正有效的获取实际天数差异。日期格式为“dd/MM/yyyy”

  string[] d1 = txtFromDate.Values.Split('/');string[] d2 = txtToDate.Values.Split('/');
DateTime FrmDt = new DateTime(Convert.ToInt32(d1[2]), Convert.ToInt32(d1[1]), Convert.ToInt32(d1[0]));DateTime ToDt = new DateTime(Convert.ToInt32(d2[2]), Convert.ToInt32(d2[1]), Convert.ToInt32(d2[0]));
TimeSpan TDiff = ToDt.Subtract(FrmDt);String DaysDiff = TDiff.TotalDays.ToString();