从DateTime减去天 [英] Subtract days from a DateTime

查看:87
本文介绍了从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 ,或者您要向 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?

这篇关于从DateTime减去天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆