Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 WinForm
Hallo Guy please i need your help.
 
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: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
You can try this.
 
To Add :- date_Of_End .AddDays(30)
 
To Subtract :- date_Of_End .Subtract(30)
 
Happy Coding
Smile | :)
  Permalink  
Comments
Umer Aziz Malik at 2-Jun-12 11:24am
   
Good answer Linto. Spot on :) +5
Linto Leo Tom at 2-Jun-12 12:03pm
   
Thanx Umer... :)
Prasad_Kulkarni at 3-Jun-12 1:32am
   
Good one +5!
Rate this: bad
good
Please Sign up or sign in to vote.

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);
DateTime date_Of_Bigin = date_Of_End.AddDays(-30);
Console.WriteLine (date_Of_Bigin.ToString("dd/MM/yyyy",
	System.Globalization.CultureInfo.InvariantCulture));
//Output
//03/05/2012
  Permalink  
Comments
Umer Aziz Malik at 2-Jun-12 11:24am
   
A much detailed and robust solution. +5
VJ Reddy at 2-Jun-12 11:51am
   
Thank you, Umer :)
Linto Leo Tom at 2-Jun-12 11:38am
   
You are absolutely right VJ...
VJ Reddy at 2-Jun-12 11:51am
   
Thank you, Linto :)
Manas Bhardwaj at 2-Jun-12 11:45am
   
nice. +5.
VJ Reddy at 2-Jun-12 11:51am
   
Thank you, Manas :)
Prasad_Kulkarni at 3-Jun-12 1:32am
   
5'ed
VJ Reddy at 3-Jun-12 2:29am
   
Thank you, Prasad :)
Rate this: bad
good
Please Sign up or sign in to vote.

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 Smile | :)
 
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);
  Permalink  
Comments
Linto Leo Tom at 2-Jun-12 11:49am
   
You spiced it with more pepper and garlic... Good one. :) +5
Manas Bhardwaj at 2-Jun-12 11:53am
   
Thanx!
VJ Reddy at 2-Jun-12 11:51am
   
Good answer. 5!
Manas Bhardwaj at 2-Jun-12 11:53am
   
Thanx!
Prasad_Kulkarni at 3-Jun-12 1:33am
   
5'ed
Manas Bhardwaj at 3-Jun-12 11:11am
   
thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 BillWoodruff 300
1 Mathew Soji 274
2 Afzaal Ahmad Zeeshan 268
3 DamithSL 225
4 Sergey Alexandrovich Kryukov 205
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 2 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100