简体   繁体   中英

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. So, for example, I want to find the date 30 days from now - what library should I look into using for this?

You can use the DateTime and TimeSpan structure.

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

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

You don't need a library, the .NET DateTime struct has all you need.

See http://msdn.microsoft.com/en-us/library/system.datetime.adddays.aspx for an explanation and example!

System.DateTime is the struct you need.

Examples:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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