【发布时间】:2015-09-29 16:35:12
【问题描述】:
我想将给定日期与今天进行比较,条件如下:如果提供的日期大于或等于从今天开始的 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 个月是我想要的 条件
- 如果少于 6 个月是另一种情况。
【问题讨论】:
-
如何精确?一天中的时间是否重要?
-
如果时间不计算现在与今天的变化
-
@WaiHaLee 仅仅几个月。谢谢。
-
2 月 30 日怎么样?
-
函数 addmonths 不会使 1 月 30 日到 2 月 30 日 -> msdn.microsoft.com/en-us/library/… 您当前的代码有什么问题?现在更改为今天并从当前日期 strDate 仅保留日期或将时间设置为 00:00:00(就像 .Today 一样)