C# 如何将一个小时添加到时间跨度。C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18746645/
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
How to add an hour to a timespan. C#
提问by Jeremy
I have my TimeSpan
for a specific reason so it HAS to be in that format. I'm trying to add an hour on to the current time. Here is what I got, which does not work:
我有TimeSpan
一个特定的原因,所以它必须是那种格式。我正在尝试在当前时间上增加一个小时。这是我得到的,但不起作用:
TimeSpan time1= TimeSpan.FromHours(1); // my attempt to add 2 hours
TimeSpan ts = DateTime.Now.TimeOfDay;
ts.Add(time1);
MessageBox.Show(ts.ToString()); // for showing me its result
Can you please advise?
你能给些建议么?
采纳答案by margabit
The method Add
of TimeSpan
is not modifying the value of ts
. It is summing the values and returning a new object.
该方法Add
的TimeSpan
不修改的值ts
。它正在对值求和并返回一个新对象。
So instead you should do:
所以你应该这样做:
TimeSpan ts = DateTime.Now.TimeOfDay;
var ts2 = ts.Add(time1);
MessageBox.Show(ts2.ToString());
回答by Daniel A. White
var newDateTime = DateTime.Now.AddHours(1);
No need to create a TimeSpan
. This will roll over to tomorrow.
无需创建TimeSpan
. 这将滚动到明天。
回答by Rahul Tripathi
Try using this:-
尝试使用这个:-
TimeSpan time1 = TimeSpan.FromHours(1); // my attempt to add 2 hours
TimeSpan ts = DateTime.Now.TimeOfDay;
var newts = ts.Add(time1);
MessageBox.Show(newts.ToString());
Try this:-
尝试这个:-
var newDateTime = DateTime.Now.AddHours(1);
回答by christiandev
Use AddHours();
使用 AddHours();
DateTime.Now.AddHours(1);
or, to use your code (see the newTs
variable)..
或者,使用您的代码(请参阅newTs
变量)。
TimeSpan time1 = TimeSpan.FromHours(1); // my attempt to add 2 hours
TimeSpan ts = DateTime.Now.TimeOfDay;
var newTs = ts.Add(time1);
MessageBox.Show(newTs.ToString());
Also, your comment says 'my attempt to add 2 hours', is this a typo?
另外,您的评论是“我尝试增加 2 小时”,这是打字错误吗?
回答by dasblinkenlight
The reason why your code does not work is that TimeSpan
is immutable. The TimeSpan.Add
method returns a new object:
您的代码不起作用的原因TimeSpan
是不可变的。该TimeSpan.Add
方法返回一个新对象:
ts = ts.Add(time1);
回答by dav_i
var newTime = oldTime.Add(new TimeSpan(1, 0, 0));
Don't fall into the common mistake of not assigning the value to anything (i.e. newTime
) as oldTime
will remain the same - TimeSpan.Add(...)
returns TimeSpan
.
不要陷入不将值分配给任何东西(即newTime
)的常见错误,因为oldTime
将保持不变 -TimeSpan.Add(...)
返回TimeSpan
。
Same applies for String.Replace(...)
- easy, but devastating, to miss.
同样适用于String.Replace(...)
- 容易,但具有破坏性,错过。
回答by Sudhanshu Mishra
My 2 cents: The question title says - How to add an hour to a timespan. C#, only one answer is accurate to that effect. Here's my take on adding hours to a time span:
我的 2 美分:问题标题说 - 如何在时间跨度中添加一个小时。C#,只有一个答案是准确的。这是我对增加时间跨度的看法:
public static TimeSpan AddHours(TimeSpan timeSpan, int hoursToAdd)
{
TimeSpan newSpan = new TimeSpan(0, hoursToAdd, 0, 0);
return timeSpan.Add(newSpan);
}
Here's a fiddle to Demo: https://dotnetfiddle.net/iuh66s
这是演示的小提琴:https: //dotnetfiddle.net/iuh66s
回答by Junpei Kun
ts += TimeSpan.FromHours(1);
and there you have it!
你有它!