计算两个日期和时间之间的差异

时间:2016-01-17 10:28:13

标签: c#

我在使用Visual Studio c#中的表单计算两天之间的差异时遇到问题。我试图使用TimeSpan,但我希望消息框显示一条消息。如何在这件事上使用if语句?

DateTime startDate = (DateTime)datePreviDate.Value;
DateTime endDate = (DateTime)datecurrentTime.Value;
TimeSpan ts = endDate.Subtract(startDate);

//Here i want to put if statemnet like 
//if the difference of days are less than 2  AND PREVTIME + CURRENT TIME
//IS LESS THEN 24 
//then MessageBox.Show.("you CANNOT CHANGE THE DATE")
//else MessageBox.Show.("you APPOINTMENT HAS BEEN CHANGED")
MessageBox.Show(ts.Days.ToString()); 

form image here

1 个答案:

答案 0 :(得分:1)

很难理解你想要的东西。但这可能会对你有所帮助。我假设" PREVTIME"和" CURRENTTIME"先前已分配。我也只是将你的意见转化为逻辑。不确定这是不是你的意思。

DateTime start =  (DateTime)datePreviDate.Value;
DateTime end = (DateTime)datecurrentTime.Value;
var timespan = end - start
var totalTime = PREVTIME + CURRENTTIME;
if(timespan.TotalDays > 2 && totalTime < 24){
   MessageBox.Show("You Cannot Change The Date");
   //Continue Code Here
} else {
   MessageBox.Show("Your Appointment Has Been Changed");
   //Continue Code Here
}