简体   繁体   English

如何从今天起x天数?

[英]How to get x number of days from today?

I'm trying to take today's date and find a date x number of days in the future. 我正在尝试获取今天的日期,并找到一个日期x将来的天数。 So, for example, I want to find the date 30 days from now - what library should I look into using for this? 因此,例如,我想查找从现在起30天的日期-我应该考虑使用哪个库?

You can use the DateTime and TimeSpan structure. 您可以使用DateTimeTimeSpan结构。

DateTime in30Days = DateTime.Today.AddDays(30);

or 要么

TimeSpan days30 = TimeSpan.FromDays(30);
DateTime in30Days = DateTime.Today + days30; 

If you need need DateTime and time-period calculations heavily, you should have a look at the Time Period library for .NET (open license) which i can recommend. 如果您需要大量的DateTime和时间周期计算,则应查看我推荐的.NET (开放许可证)的Time Period库

使用DateTime

DateTime _x = DateTime.Today.AddDays(30);

You don't need a library, the .NET DateTime struct has all you need. 您不需要库,.NET DateTime结构即可满足您的所有需求。

See http://msdn.microsoft.com/en-us/library/system.datetime.adddays.aspx for an explanation and example! 有关说明和示例,请参见http://msdn.microsoft.com/zh-cn/library/system.datetime.adddays.aspx

System.DateTime is the struct you need. System.DateTime是您需要的结构。

Examples: 例子:

DateTime.Today.AddDays(30); //30 days forwards
DateTime.Today.AddDays(-30); //30 days backwards.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM