我看到Java,JavaScript和PHP已经回答了这个问题,但没有回答C#。那么,如何计算C#中两个日期之间的天数?
答案 0 :(得分:1778)
假设StartDate
和EndDate
类型为DateTime
:
(EndDate - StartDate).TotalDays
答案 1 :(得分:129)
使用TimeSpan对象,它是日期减法的结果:
DateTime d1;
DateTime d2;
return (d1 - d2).TotalDays;
答案 2 :(得分:99)
最佳答案是正确的,但是如果你只希望整个日子作为一个整数并且乐于放弃约会的时间部分,那么请考虑:
$headers = array(
"Authorization: Basic " . base64_encode($apidID . ':' . $apiSkey)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
再次假设StartDate和EndDate的类型为DateTime。
答案 3 :(得分:43)
我认为这会做你想做的事情:
DateTime d1 = DateTime.Now;
DateTime d2 = DateTime.Now.AddDays(-1);
TimeSpan t = d1 - d2;
double NrOfDays = t.TotalDays;
答案 4 :(得分:34)
DateTime xmas = new DateTime(2009, 12, 25);
double daysUntilChristmas = xmas.Subtract(DateTime.Today).TotalDays;
答案 5 :(得分:17)
如果有人想要将整天的天数作为双倍a
,b
类型DateTime
):
(a.Date - b.Date).TotalDays
答案 6 :(得分:13)
// 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
您还可以获得秒,毫秒和刻度的差异。
答案 7 :(得分:8)
你可以试试这个
println(char[])
答案 8 :(得分:4)
对于像我这样的初学者,会在一个简单的行中偶然发现这个小问题,将样本转换为 int :
int totalDays = Convert.ToInt32((DateTime.UtcNow.Date - myDateTime.Date).TotalDays);
这会计算从今天起的总天数(DateTime.UtcNow.Date)到所需的日期(myDateTime.Date)。
如果myDateTime是昨天或更早的日期,那么这将给出正(+)整数结果。
另一方面,如果myDateTime是明天或将来的日期,由于添加规则,这将给出负( - )整数结果。
快乐的编码! ^ _ ^
答案 9 :(得分:3)
对于a
和b
两种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();
答案 10 :(得分:2)
使用时间跨度可以解决问题,因为它有许多属性:
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();
答案 11 :(得分:0)
首先声明一个稍后会返回的类:
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();
}
使用按钮控件调用上述类。这是一个例子:
答案 12 :(得分:0)
获取两个日期之间的差异,然后从以下日期获取:
int total_days = (EndDate - StartDate).TotalDays
答案 13 :(得分:0)
您可以使用以下代码:
re.findall
答案 14 :(得分:-20)
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;
}
}