从DateTime减去天
我的C#程序中有以下代码。
I have the following code in my C# program.
DateTime dateForButton = DateTime.Now;
dateForButton = dateForButton.AddDays(-1); // ERROR: un-representable DateTime
每当我运行它时,都会出现以下错误:
Whenever I run it, I get the following error:
相加或相减的值导致日期时间无法表示。
参数名称:值
以前我从未见过此错误消息,也不知道为什么我看到了从到目前为止的答案中,我可以认为我可以在加法运算中使用-1来减去天数,但是由于我的问题表明我尝试做的情况并非如此。 / p>
Iv'e never seen this error message before, and don't understand why I'm seeing it. From the answers Iv'e read so far, I'm lead to believe that I can use -1 in an add operation to subtract days, but as my question shows this is not the case for what I'm attempting to do.
当您尝试从 DateTime.MinValue $ c $减去一个间隔时,通常会发生该错误。 c>,或者您要向
DateTime.MaxValue
添加一些内容(或尝试实例化此最小-最大间隔之外的日期)。您确定没有在某处分配 MinValue
吗?
That error usually occurs when you try to subtract an interval from DateTime.MinValue
or you want to add something to DateTime.MaxValue
(or you try to instantiate a date outside this min-max interval). Are you sure you're not assigning MinValue
somewhere?