C# 为什么 DateTime.AddHours 似乎不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10781420/
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 15:14:50  来源:igfitidea点击:

Why DateTime.AddHours doesn't seem to work?

c#.netdatetime

提问by user310291

I have same result 1338161400 when I do

当我这样做时,我得到了相同的结果 1338161400

    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    DateTime date = DateTime.Parse(@"28/05/12 01:30");
    TimeSpan diff = date.ToUniversalTime() - origin;
    Console.WriteLine( (Math.Floor(diff.TotalSeconds)).ToString());

as well as when I use date.AddHours(-4) :

以及当我使用 date.AddHours(-4) 时:

    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    DateTime date = DateTime.Parse(@"28/05/12 01:30");
    date.AddHours(-4);
    TimeSpan diff = date.ToUniversalTime() - origin;
    Console.WriteLine( (Math.Floor(diff.TotalSeconds)).ToString());

I try to get 1338168600 like http://www.mbari.org/staff/rich/utccalc.htm

我尝试像http://www.mbari.org/staff/rich/utccalc.htm一样获得 1338168600

Update:

更新:

Thanks I changed to

谢谢我改成

    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    DateTime date = DateTime.Parse(@"28/05/2012 01:30");
    date = date.AddHours(-4);
    date = date.ToUniversalTime();
    TimeSpan diff = date - origin;
    Console.WriteLine((Math.Floor(diff.TotalSeconds)).ToString());  

But I got 1338147000 still not 1338168600

但我得到 1338147000 仍然不是 1338168600

采纳答案by Slugart

Dates are immutable objects, i.e. they cannot be modified after creation. DateTime.AddHours returns a new DateTime instance which is shifted backwards by 4 hours but "date" will not be modified.

日期是不可变的对象,即它们在创建后不能被修改。DateTime.AddHours 返回一个新的 DateTime 实例,该实例向后移动 4 小时,但不会修改“日期”。

Use:

用:

DateTime newDate = date.AddHours(-4);

回答by Rawling

AddHoursreturns a newDateTimeobject which is the result of adding the hours onto the original. The original is left unchanged.

AddHours返回一个DateTime对象,它是将小时添加到原始对象的结果。原件保持不变。

Thus you want date = date.AddHours(-4);instead of just date.AddHours(-4);

因此你想要date = date.AddHours(-4);而不是仅仅date.AddHours(-4);

回答by Lloyd Powell

AddHourssimply returns the new incremented date, use this :

AddHours简单地返回新的递增日期,使用这个:

date = date.AddHours(-4);

回答by Adam

AddHours returns new DateTime object. do this:

AddHours 返回新的 DateTime 对象。做这个:

date = date.AddHours(-4);

Documentation: link

文档:链接

回答by John Woo

The original date is set 4 hours backward but you didn't set it to new date. try this instead:

原始日期设置为向后 4 小时,但您没有将其设置为新日期。试试这个:

date = date.AddHours(-4);

回答by N73k

DateTime is not immutable. It's just that the DateTime methods don't change the struct, but instead return a new one.

日期时间不是一成不变的。只是 DateTime 方法不会更改结构,而是返回一个新结构。

If you do this:

如果你这样做:

DateTime d1 = DateTime.Now;
DateTime d2 = DateTime.Now;
d1 = d2;

Then, the struct d1 will be overwritten with d2's values.

然后,结构 d1 将被 d2 的值覆盖。

I don't know if it's possible to have an immutable struct.

我不知道是否有可能有一个不可变的结构。

回答by Sameeksha Kumari

Try if this works:

试试这是否有效:

System.DateTime today = System.DateTime.Now;
System.TimeSpan duration = new System.TimeSpan(0, 11, 0, 0);
System.DateTime answer = today.Add(duration);
System.Console.WriteLine("{0:dddd}", answer);

This seems to change the day in the date also on adding hours.

这似乎也会在增加小时数时更改日期中的日期。