c# 計算兩個日期之間的差異（天數）？

``````DateTime d1;
DateTime d2;
return (d1 - d2).TotalDays;
``````
c# date

``````DateTime d1 = DateTime.Now;

TimeSpan t = d1 - d2;
double NrOfDays = t.TotalDays;
``````

`````` (a.Date - b.Date).TotalDays
``````

``````// 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
``````

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

``````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();
}
``````

``````int total_days = (EndDate - StartDate).TotalDays
``````

c#   date