C# 从日期时间中减去天数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11151976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:45:04  来源:igfitidea点击:

Subtract days from a DateTime

c#datetime

提问by Buena

I have the following code in my C# program.

我的 C# 程序中有以下代码。

DateTime dateForButton =  DateTime.Now;  
dateForButton = dateForButton.AddDays(-1);  // ERROR: un-representable DateTime

Whenever I run it, I get the following error:

每当我运行它时,都会出现以下错误:

The added or subtracted value results in an un-representable DateTime.
Parameter name: value

添加或减去的值会导致无法表示的 DateTime。
参数名称:值

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.

我以前从未见过此错误消息,也不明白为什么我会看到它。从到目前为止我读到的答案中,我相信我可以在加法运算中使用 -1 来减去天数,但正如我的问题所显示的那样,我尝试做的事情并非如此。

采纳答案by CyberDude

That error usually occurs when you try to subtract an interval from DateTime.MinValueor 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 MinValuesomewhere?

该错误通常发生在您尝试从中减去一个间隔DateTime.MinValue或想要添加某些内容DateTime.MaxValue(或者您尝试实例化此最小-最大间隔之外的日期)时。你确定你不是在分配MinValue某个地方吗?

回答by Rajesh Subramanian

You can use the following code:

您可以使用以下代码:

dateForButton = dateForButton.Subtract(TimeSpan.FromDays(1));

回答by christiandev

DateTime dateForButton = DateTime.Now.AddDays(-1);

回答by sam

You can do:

你可以做:

DateTime.Today.AddDays(-1)

回答by Mark Benson

The object (i.e. destination variable) for the AddDays method can't be the same as the source.

AddDays 方法的对象(即目标变量)不能与源相同。

Instead of:

代替:

DateTime today = DateTime.Today;
today.AddDays(-7);

Try this instead:

试试这个:

DateTime today = DateTime.Today;
DateTime sevenDaysEarlier = today.AddDays(-7);

回答by cahit beyaz

The dateTime.AddDays(-1)does not subtract that one day from the dateTimereference. It will return a new instance, with that one day subtracted from the original reference.

dateTime.AddDays(-1)不减去一天,有一个dateTime参考。它将返回一个新实例,从原始引用中减去这一天。

DateTime dateTime = DateTime.Now;
DateTime otherDateTime = dateTime.AddDays(-1);

回答by Ainsley Hobart

Using AddDays(-1)worked for me until I tried to cross months. When I tried to subtract 2 days from 2017-01-01 the result was 2016-00-30. It could not handle the month change correctly (though the year seemed to be fine).

使用AddDays(-1)对我有用,直到我试图跨越几个月。当我尝试从 2017-01-01 减去 2 天时,结果是 2016-00-30。它无法正确处理月份变化(尽管年份似乎很好)。

I used date = Convert.ToDateTime(date).Subtract(TimeSpan.FromDays(2)).ToString("yyyy-mm-dd");and have no issues.

我用过date = Convert.ToDateTime(date).Subtract(TimeSpan.FromDays(2)).ToString("yyyy-mm-dd");,没有问题。

回答by UsernameNotFound

I've had issues using AddDays(-1).

我在使用AddDays(-1) 时遇到了问题。

My solution is TimeSpan.

我的解决方案是TimeSpan

DateTime.Now - TimeSpan.FromDays(1);

回答by Sambhav jain

Instead of directly decreasing number of days from the date object directly, first get date value then subtract days. See below example:

不是直接从日期对象中直接减少天数,而是先获取日期值然后减去天数。见下面的例子:

DateTime SevenDaysFromEndDate = someDate.Value.AddDays(-1);

Here, someDate is a variable of type DateTime.

这里,someDate 是一个 DateTime 类型的变量。