【发布时间】:2017-04-18 16:31:00
【问题描述】:
如果NextInspectionDueDate 小于或等于今天的日期,那么我想将它的值更新为同一天但明年,特别是四月。
我尝试了以下方法,但它给出了“有一些无效参数”错误。 我只是想不出正确的语法。也许我应该换一种方式?
if (foundVehicle.Vehicle.NextInspectionDueDate <= DateTime.Now)
{
foundVehicle.Vehicle.NextInspectionDueDate = new DateTime(DateTime.Now.AddYears(1), 4);
}
【问题讨论】:
-
如果今天是
31 Jan怎么办?结果应该是30 Apr还是1 May? -
您必须输入
Day:... new DateTime(DateTime.Now.AddYears(1), 4, 1);才能拥有1 Apr 2018 -
DateTime.Now.AddYears返回System.DateTime。你需要一个int。 -
附注:也许Noda Time 你也很感兴趣。
-
@Johnny Mopp,正确,提供的示例代码甚至无法编译