C# 将 1 周添加到当前日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10036413/
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
Add 1 week to current date
提问by René Bene?
I've got something like this DateTime.Now.ToString("dd.MM.yy");In my code, And I need to add 1 week to it, like 5.4.2012to become 12.4.2012I tried to convert it to int and then add it up, but there is a problem when it's up to 30.
我有这样的事情DateTime.Now.ToString("dd.MM.yy");在我的代码,我需要1周添加到它,喜欢5.4.2012成为12.4.2012我试图将其转换为int,然后添加它,但没有当它是多达30个问题。
Can you tell me some clever way how to do it?
你能告诉我一些聪明的方法吗?
采纳答案by Guvante
You want to leave it as a DateTimeuntil you are ready to convert it to a string.
您希望将其保留为 a,DateTime直到您准备好将其转换为字符串。
DateTime.Now.AddDays(7).ToString("dd.MM.yy");
回答by Dan P
Any reason you can't use the AddDays method as in
您不能使用 AddDays 方法的任何原因
DateTime.Now.AddDays(7)
回答by mgnoonan
First, always keep the data in it's native type until you are ready to either display it or serialize it (for example, to JSON or to save in a file). You wouldn't convert two intvariables to strings before adding or multiplying them, so don't do it with dates either.
首先,始终将数据保留在本机类型中,直到您准备好显示或序列化它(例如,转换为 JSON 或保存在文件中)。在将两个int变量相加或相乘之前,您不会将它们转换为字符串,因此也不要对日期进行转换。
Staying in the native type has a few advantages, such as storing the DateTimeinternally as 8 bytes, which is smaller than most of the string formats. But the biggest advantage is that the .NET Framework gives you a bunch of built in methods for performing date and time calculations, as well as parsing datetime values from a source string. The full list can be found here.
留在本机类型有一些优点,例如在DateTime内部存储为 8 个字节,这比大多数字符串格式要小。但最大的优势是 .NET Framework 为您提供了一系列用于执行日期和时间计算以及从源字符串解析日期时间值的内置方法。完整列表可以在这里找到。
So your answer becomes:
所以你的答案变成了:
- Get the current timestamp from
DateTime.Now. UseDateTime.Now.Dateif you'd rather use midnight than the current time. - Use
AddDays(7)to calculate one week later. Note that this method automatically takes into account rolling over to the next month or year, if applicable. Leap days are also factored in for you. - Convert the result to a string using your desired format
- 从 获取当前时间戳
DateTime.Now。使用DateTime.Now.Date,如果你想用午夜比当前时间。 - 使用
AddDays(7)一周后计算。请注意,此方法会自动考虑滚动到下个月或下一年(如果适用)。闰日也考虑在内。 - 使用您想要的格式将结果转换为字符串
// Current local server time + 7 days
DateTime.Now.AddDays(7).ToString("dd.MM.yy");
// Midnight + 7 days
DateTime.Now.Date.AddDays(7).ToString("dd.MM.yy");
And there are plenty of other methods in the framework to help with:
框架中还有很多其他方法可以帮助:
- Internationalization
- UTC and timezones (though you might want to check out NodaTimefor more advanced applications)
- Operator overloading for some basic math calcs
- The
TimeSpanclass for working with time intervals
- 国际化
- UTC 和时区(尽管您可能想查看NodaTime以获得更高级的应用程序)
- 一些基本数学计算的运算符重载
- 在
TimeSpan与时间间隔工人阶级

