12,077,594 members (48,780 online)
Rate this:
See more:

I want to calculate a difference between a date and a duration like this.
```DateTime date_Of_End = new DateTime(2012, 6, 02);
int duration  = 30;

DateTime date_Of_Bigin = date_Of_End - duration ;
```

Please i get a failure how can i do? have somebody an idea??
Posted 2-Jun-12 5:20am

Rate this:

## Solution 1

Hi,

You can try this.

To Subtract :- date_Of_End .Subtract(30)

Happy Coding
Umer Aziz Malik 2-Jun-12 11:24am

Good answer Linto. Spot on :) +5
Linto Leo Tom 2-Jun-12 12:03pm

Thanx Umer... :)

Good one +5!
Rate this:

## Solution 2

If it is required to deduct `30 days` from the `Date`, then I think the `AddDays `method of `DateTime `class with negative number can be used as the `Substract `method of `DateTime `accepts a `DateTime `object and not a duration.
```DateTime date_Of_End = new DateTime(2012, 6, 02);
Console.WriteLine (date_Of_Bigin.ToString("dd/MM/yyyy",
System.Globalization.CultureInfo.InvariantCulture));
//Output
//03/05/2012```
Umer Aziz Malik 2-Jun-12 11:24am

A much detailed and robust solution. +5
VJ Reddy 2-Jun-12 11:51am

Thank you, Umer :)
Linto Leo Tom 2-Jun-12 11:38am

You are absolutely right VJ...
VJ Reddy 2-Jun-12 11:51am

Thank you, Linto :)
Manas Bhardwaj 2-Jun-12 11:45am

nice. +5.
VJ Reddy 2-Jun-12 11:51am

Thank you, Manas :)

5'ed
VJ Reddy 3-Jun-12 2:29am

Rate this:

## Solution 3

Adding to what VJ Reddy explained, I usually prefer to use TimeSpan[^]. It gives you flexibility when you want to have more accurate calculations. For eg. 30 days, 10 hours and 45 mins

```DateTime date_Of_End = new DateTime(2012, 6, 02);

int duration  = 30;

TimeSpan tsDay = newTimeSpan(duration, 0, 0, 0);

DateTime date_Of_Bigin =  date_Of_End.Subtract(tsDay);```
Linto Leo Tom 2-Jun-12 11:49am

You spiced it with more pepper and garlic... Good one. :) +5
Manas Bhardwaj 2-Jun-12 11:53am

Thanx!
VJ Reddy 2-Jun-12 11:51am

Manas Bhardwaj 2-Jun-12 11:53am

Thanx!

5'ed
Manas Bhardwaj 3-Jun-12 11:11am

thanks!

Top Experts
Last 24hrsThis month
 Richard MacCutchan 365 Sergey Alexandrovich Kryukov 278 Jochen Arndt 220 OriginalGriff 170 Richard Deeming 105
 OriginalGriff 2,965 Dave Kreskowiak 2,836 Richard MacCutchan 2,249 CPallini 1,632 Sergey Alexandrovich Kryukov 1,445