Date functions in c#

  • Thread starter Thread starter Kiran
  • Start date Start date
K

Kiran

Hi,

Please help on date functions in C#.

I would like to know the relevant exampl to retrieve
2 weeks before date from the current date,
1 month before date from the current date and number of days.
2 months before date from the current date and number of days.
3 months before date from the current date and number of days.
8 months before date from the current date and number of days.
1 year before date from the current date and number of days.

Pls help...

Regards,
Krian
 
You should take a look at the System.DateTime and System.TimeSpan
documentation.

Bruno.
 
Hi! You can use someting like

DateTime dt = DateTime.Today.AddMonths(-2).AddDays(-3);

//Today 2 months before and 3 days

or

DateTime dt = DateTime.Today.AddYears(-1).AddMonths(-2).AddDays(-3);

//One year , two months and 3 days before.
 
Please help on date functions in C#.

Have a look at the DateTime object, then its AddDays, AddWeeks, AddMonths
and AddYears methods. To go back in time, you just "add" a negative amount
of units.
 
Have you looked at all the Add* methods in the DateTime structure. Just
pass a negative value if you want dates before the specified date.
 
Kiran said:
Hi,

Please help on date functions in C#.

I would like to know the relevant exampl to retrieve
2 weeks before date from the current date,
1 month before date from the current date and number of days.
2 months before date from the current date and number of days.
3 months before date from the current date and number of days.
8 months before date from the current date and number of days.
1 year before date from the current date and number of days.

DateTime.AddXXX

EG current date minus one month and 6 days.

DateTime.Now.Date.AddMonths(-1).AddDays(-6)

David
 
Back
Top