Michael A
Michael A

Reputation: 9930

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?

Upvotes: 0

Views: 2221

Answers (4)

RRR
RRR

Reputation: 116

System.DateTime is the struct you need.

Examples:

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

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460288

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.

Upvotes: 5

Rudolf Mühlbauer
Rudolf Mühlbauer

Reputation: 2531

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!

Upvotes: 1

John Woo
John Woo

Reputation: 263893

use DateTime

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

Upvotes: 2

Related Questions