添加分钟到日期不会改变日期 C# .NET

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

Adding Minutes to Date is not changing the Date C# .NET

c#datetime

提问by

double TotalMinute=300.0 double TotalMinutesAdded=1378.0

double TotalMinute=300.0 double TotalMinutesAdded=1378.0


double TotalMinute=300.0
double TotalMinutesAdded=1378.0

foreach(DataRow dr in ds.Tables[0].Rows)
                {

                    //Add The above Timings to each Row's 2nd Column
                    DateTime correctDate=Convert.ToDateTime(dr[2]);

                    correctDate.AddMinutes(TotalMinute);
                    correctDate.AddMinutes(TotalMinutesAdded);

                    dr[2]=correctDate;

                }

采纳答案by Garry Shutler

As mentioned, due to DateTimeobjects being immutable you have to reassign the variable.

如前所述,由于DateTime对象是不可变的,您必须重新分配变量。

However, a point to note is that you can chain the manipulations as so:

但是,需要注意的一点是,您可以像这样链接操作:

correctDate = correctDate.AddMinutes(TotalMinute)
                         .AddMinutes(TotalMinutesAdded);

回答by Janis Veinbergs

DateTiem Add* functions are not supposed to change current DateTime value. They RETURN the new Value.

DateTiem Add* 函数不应更改当前的 DateTime 值。他们返回新的价值。

If you want your value changed, type like this:

如果你想改变你的值,像这样输入:

correctDate = correctDate.AddMinutes(TotalMinute);

回答by Todd

You have to set the correctDate variable to instance returned from the AddMinutes call:

您必须将正确日期变量设置为从 AddMinutes 调用返回的实例:

correctDate = correctDate.AddMinutes(TotalMinute);

回答by Sebastian Dietz

AddMinutes() does not change the value of the original DateTime. It returns a new DateTime with the new value that you have to assign to a variable.

AddMinutes() 不会更改原始 DateTime 的值。它返回一个新的 DateTime,其中包含您必须分配给变量的新值。

回答by Marc Gravell

DateTime is immutable; functions like AddMinutes return a newDateTime; so you need to catch the returned value:

日期时间是不可变的;AddMinutes 等函数返回一个新的DateTime;所以你需要捕捉返回的值:

DateTime foo = ...
DateTime bar = foo.AddMinutes(5);

回答by efdee

DateTime is an immutable type, much like String is. You would write date = date.AddDays(1) like you would write str = str.Replace("hello", "").

DateTime 是一个不可变类型,很像 String 。您可以像编写 str = str.Replace("hello", "") 一样编写 date = date.AddDays(1)。

回答by NR.

The problem lies with

问题在于

correctDate.AddMinutes(TotalMinute); 
correctDate.AddMinutes(TotalMinutesAdded);

it should be

它应该是

correctDate = correctDate.AddMinutes(TotalMinute); 
correctDate = correctDate.AddMinutes(TotalMinutesAdded);

The AddMinutes method returns the result, not adding the minutes to correctDate

AddMinutes 方法返回结果,而不是将分钟添加到correctDate

回答by Kristian

...and to add say 5 minutes to the current time to a datetime variable:

...并将当前时间添加 5 分钟到日期时间变量:

dim dateFive_Minute_Time as datetime

dateFive_Minute_Time = Now.AddMinutes(5)