计算两个date之间的差异(天数)?
我发现这个问题已经被Java , JavaScript和PHP所回答,但不是C#。 那么,如何计算C#中两个date之间的天数呢?
假设StartDate
和EndDate
的types为DateTime
:
(EndDate - StartDate).TotalDays
使用作为date减法结果的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;
最好的答案是正确的,但是如果你只想把整个天作为一个整数并乐意放弃date的时间部分,那么考虑:
(EndDate.Date - StartDate.Date).Days
再次假设StartDate和EndDate的types为DateTime:
DateTime xmas = new DateTime(2009, 12, 25); double daysUntilChristmas = xmas.Subtract(DateTime.Today).TotalDays;
万一有人想把整天的数字作为一个double( a
, b
types的DateTime
):
(a.Date - b.Date).TotalDays
你可以试试这个
EndDate.Date.Subtract(DateTime.Now.Date).Days
//天,小时和分钟的差异。
TimeSpan ts = EndDate - StartDate;
//天数差异
int differenceInDays = ts.Days; //This is in int double differenceInDays= ts.TotalDays; //This is in double
//时差。
int differenceInHours = ts.Hours; //This is in int double differenceInHours= ts.TotalHours; //This is in double
//分钟差异
int differenceInMinutes = ts.Minutes; //This is in int double differenceInMinutes= ts.TotalMinutes; //This is in double
你也可以得到秒,毫秒和滴答的差异。
对于a
和b
作为两个DateTime
types:
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(); }
使用button控件来调用上面的类。 这里是一个例子:
您可以使用下面的代码:
int DateDifInSecond = EndDate.Subtract(StartDate).TotalSeconds
获取两个date之间的差异,然后从以下date获取:
int total_days = (EndDate - StartDate).TotalDays
使用时间跨度可以解决问题,因为它有很多属性
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)到所需date(myDateTime.Date)的总天数。
如果myDateTime是昨天或比今天更早的date,这将给出正(+)整数结果。
另一方面,如果myDateTime是明天或将来的date,则由于添加规则,这将给出负( – )整数结果。
快乐的编码! ^ _ ^
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; } }