我有 2 个日期时间选择器,如果用户选择日期,我想在文本框中显示它们之间的天数。我的代码的问题是它没有给我正确的答案,而且时间跨度似乎不起作用。我不知道我哪里出错了,这就是我寻求帮助的原因。我希望解释得更好,请多多包涵,这是我第一次来这个网站,所以我不熟悉控件、发送东西和更新当我选择不同的日期时,它会给我答案 10.999998008713 天而不是 11 天,我不知道我是否需要做数学综述
private void btnCalc_Click(object sender, EventArgs e)
{
DateTime start = ArrivalDate.Value;
DateTime finish = DepartureDate.Value;
TimeSpan numberOfNights = finish-start;
double TotalDays= numberOfNights.Days;
txtBoxNum.Text = (numberOfNights.ToString());
}
private void ArrivalDate_ValueChanged(object sender, EventArgs e)
{
DepartureDate.Value = ArrivalDate.Value.AddDays(1);
}
private void DepartureDate_ValueChanged(object sender, EventArgs e)
{
// setting messagebox to a sensible default message if no date or wrong date picked
if (DepartureDate.Value < ArrivalDate.Value)
{
MessageBox.Show("Cannot be less than previous date");
DepartureDate.Value = ArrivalDate.Value.AddDays(1);
}
else
{
double Days = (DepartureDate.Value - ArrivalDate.Value).TotalDays;
txtBoxNum.Text = Days.ToString();
return;