C#.NET中的问题求和日期 [英] Problem summing dates in C# .NET
本文介绍了C#.NET中的问题求和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我有这个问题
我需要加1天的总和,但我不知道该怎么做.
结果应为:
天+ 1/月/年
谢谢
Hello everyone
I have this problem
I need sum the day + 1 and I don''t know how to do that.
The result should be:
Day + 1/Month/Year
Thanks
推荐答案
DateTime today = DateTime.Now.Date;
DateTime tomorrow = today.AddDays(1);
DateTime myDateTime = DateTime.Now;
DateTime newDateTime = myDateTime.AddDays(1);
如果您只阅读过文档 [
If only you had read the documentation[^] you might have found such an elementary thing for yourself.
更一般而言,您可以添加/减去System.TimeSpan
来涵盖任何可能的时间段.
More generally, you can add/subtractSystem.TimeSpan
to cover any thinkable periods of time.
DateTime before = DateTime.Now;
//wait a bit
DateTime after = DateTime.Now;
TimeSpan duration = after - before;
TimeSpan extraTime = new TimeSpan(2, 12, 21); //2:12:21 hours:minutes:seconds
duration += extraTime; //defined add/subtract for TimeSpan
before += duration; //defined add/subtract for DateTime and TimeSpan (must be right operand)
before = before - duration;
//will not compile, not defined, of course:
//duration = duration - after;
这篇关于C#.NET中的问题求和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文