0

嗨,我正在尝试在 Visual Studio 中创建一个简单的预订表格,该表格需要一个到达日期和离开日期,获取持续时间并通过将持续时间乘以 115 英镑来计算住宿费用,这是我的代码(错误在线弹出开始“ int nights = dateDiff”并说“Timespan 无法转换为 int) 提前感谢您的帮助:

        String arrival, departure;
        arrival = textBox1.Text;
        departure = textBox2.Text;

        DateTime aDate, dDate;
        aDate = new DateTime();
        aDate = DateTime.ParseExact(arrival, "dd/mm/yyyy", null);
        dDate = new DateTime();
        dDate = DateTime.ParseExact(arrival, "dd/mm/yyyy", null);

        TimeSpan dateDiff;
        dateDiff = dDate.Subtract(aDate);
        int nights = dateDiff;

        textBox3.Text = ("" + nights);
        textBox5.Text = ("£" + (nights * 115));
4

1 回答 1

1

尝试

int nights = (int) dateDiff.TotalDays;

请参阅MSDN。这可能会有所帮助(即(int) Math.Round(dateDiff.TotalDays)- 请参阅此处

于 2012-11-16T10:46:51.333 回答