我希望将给定日期与今天进行比较,并根据以下条件进行判断:如果提供的日期早于或等于今天6个月以前,则返回true,否则返回false。
代码:
string strDate = tbDate.Text; //2015-03-29
if (DateTime.Now.AddMonths(-6) == DateTime.Parse(strDate)) //if given date is equal to exactly 6 months past from today (change == to > if date has to be less 6 months)
{
lblResult.Text = "true"; //this doesn't work with the entered date above.
}
else //otherwise give me the date which will be 6 months from a given date.
{
DateTime dt2 = Convert.ToDateTime(strDate);
lblResult.Text = "6 Months from given date is: " + dt2.AddMonths(6); //this works fine
}
- 如果一个条件需要6个月或更长时间,那么就满足这个条件。
- 如果少于6个月,则满足另一个条件。