Calculate difference between two dates (number of days)?
我发现这个问题已经被Java、JavaScript和PHP所回答,但不是C语言。那么,如何计算两个日期之间的天数呢?
assuming
1 | (EndDate - StartDate).TotalDays |
这是使用的TimeSpan对象的结果,substraction:日期
1 2 3 | DateTime d1; DateTime d2; return (d1 - d2).TotalDays; |
答案是一个正确的顶上,但是如果你只会一天样的全是快乐的forgo int和对组件的时间日期,然后考虑。
1 | (EndDate.Date - StartDate.Date).Days |
又是assuming startdate EndDate型和日期时间。
我想这会的你想要的。
1 2 3 4 5 | DateTime d1 = DateTime.Now; DateTime d2 = DateTime.Now.AddDays(-1); TimeSpan t = d1 - d2; double NrOfDays = t.TotalDays; |
1 2 | DateTime xmas = new DateTime(2009, 12, 25); double daysUntilChristmas = xmas.Subtract(DateTime.Today).TotalDays; |
有人想在结婚之日计算的双(全
1 | (a.Date - b.Date).TotalDays |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // Difference in days, hours, and minutes. TimeSpan ts = EndDate - StartDate; // Difference in days. int differenceInDays = ts.Days; // This is in int double differenceInDays= ts.TotalDays; // This is in double // Difference in Hours. int differenceInHours = ts.Hours; // This is in int double differenceInHours= ts.TotalHours; // This is in double // Difference in Minutes. int differenceInMinutes = ts.Minutes; // This is in int double differenceInMinutes= ts.TotalMinutes; // This is in double |
你也可以在一秒的时间差,milliseconds和蜱。
你可以试试这个
1 | EndDate.Date.Subtract(DateTime.Now.Date).Days |
我将stumble初学者在这样简单的小问题,在转换到线,与样品中:
1 | int totalDays = Convert.ToInt32((DateTime.UtcNow.Date - myDateTime.Date).TotalDays); |
这一天从今天的calculates(datetime.utcnow.date)到所需的日期(mydatetime.date)。
如果是新的或旧的mydatetime比昨天,今天,这将给阳性(+)的整数结果。
在其他方面,如果mydatetime是明天或未来日期的在线,这将给负(-)整数加法的结果导致的规则。
编码快乐!_ ^ ^
二是
1 2 3 4 5 6 7 8 9 | 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(); |
使用的时间解决问题,它有许多的属性:
1 2 3 4 5 6 | 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(); |
你可以使用下面的代码:
1 | int DateDifInSecond = EndDate.Subtract(StartDate).TotalSeconds |
得到两个日期之间的差异,然后从一天。
1 | int total_days = (EndDate - StartDate).TotalDays |
第一类是将返回后,宣布:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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(); } |
使用呼叫按钮控制上述类。这里是一个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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("alert(' Not a valid Date to extend warranty')"); } else { string diff = dt.Subtract(dt1).ToString(); Response.Write(diff); Label18.Text = diff; Session["diff"] = Label18.Text; } } |