Click here to Skip to main content
16,016,744 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I want to take a difference between two date and convert it into the date
like in 10/28/2010 add 7 days so the next date is 11/04/2010 so how to calculate the date ?


I mean that if a month at the End and we select the 29th if I add 8 so the result 36 but 36 is not possible date.


Next Que.

how to calculate the quarter of the year from the starting date to Next Date like first Quarter is Start from April to Aug. so the first date of quarter and the Last Date of Quarter

Please help.
thanks in advance.
Posted
Updated 22-Oct-10 18:27pm
v3
Comments
Rajesh Anuhya 22-Oct-10 8:12am    
your question is not clear for me, how the two dates difference become a Date???
shakil0304003 22-Oct-10 12:10pm    
what is your requirement? days difference between two date!!! Or add 7/some days to a date? Clear it?
Mohd Wasif 23-Oct-10 1:23am    
Let me know whether u r working with sql server 2005 or with asp.net
Bikash Shrestha From Nepal 23-Oct-10 2:02am    
Sorry bro,please make yourself clear

To calculate the quarter you can use the Time Period Library for .NET:
C#
// ----------------------------------------------------------------------
public static void ShowQuarterInfo()
{
  ShowQuarterInfo( DateTime.Now );
} // ShowQuarterInfo

// ----------------------------------------------------------------------
public static void ShowQuarterInfo( DateTime moment )
{
  // working quarter
  Quarter quarter = new Quarter( moment );
  Console.WriteLine( "Quarter start: " + quarter.FirstDayStart );
  Console.WriteLine( "Quarter end: " + quarter.LastDayStart );

  // previous quarter
  Quarter previousQuarter = quarter.GetPreviousQuarter();
  Console.WriteLine( "Previous Quarter start: " + previousQuarter.FirstDayStart );
  Console.WriteLine( "Previous Quarter end: " + previousQuarter.LastDayStart );

  // next quarter
  Quarter nextQuarter = quarter.GetNextQuarter();
  Console.WriteLine( "Next Quarter start: " + nextQuarter.FirstDayStart );
  Console.WriteLine( "Next Quarter end: " + nextQuarter.LastDayStart );
} // ShowQuarterInfo


You will find more samples in the article Time Period Library for .NET[^].

Cheers, Jani.
 
Share this answer
 
Comments
Jani Giannoudis 18-Mar-11 19:23pm    
See also this question: http://www.codeproject.com/Questions/120609/Calculate-the-Date-Difference-and-Convert-into-Wee.aspx.
Use AddDays Like

DateTime mydt = DateTime.Today.Date.AddDays(7);
 
Share this answer
 
Comments
Abhinav S 22-Oct-10 8:50am    
Good answer.
Dear,

U can't make the difference between two dates in date form.
but u can get date difference in sec,min,hr,days,months,years.
So In order to get difference b/n two dates we need to do concatenation,but the things is this if we take difference of same day,same month and same year then we will not date in date form coz in each case we will get 0 for days,months and years.That will look like 00\00\0000. Any thing can be zero as per your choice.So if we add 7 days then we will not get proper result

E.G
select dateadd(d,+5, datediff(d,getdate(),getdate()+1) )
this gives 1900-01-07 00:00:00.000
coz
select datediff(d,getdate(),getdate()+1)
it gives 1.
if we add 7 days then we will get
1900-01-07
 
Share this answer
 
Your requirement -> "I want to take a difference between two date and convert it into the date". And it's solution ->
DateTime d1 = new DateTime(2010, 10, 28);
            DateTime d2 = new DateTime(2010,11,04);
            TimeSpan p1 = d2.Subtract(d1);
            MessageBox.Show(p1.Days.ToString());
 
Share this answer
 
Comments
William Winner 22-Oct-10 12:13pm    
While his original question was unclear, I think it was clear that he was saying, he wants to take a date and add a number of days to the first date and get the second date.

"like in 10/28/2010 add 7 days so the next date is 11/04/2010"
shakil0304003 22-Oct-10 12:26pm    
If your say is right, then why he said "I want to take a difference between two date and convert it into the date". Explain why he said like this?
William Winner 22-Oct-10 13:17pm    
probably because English isn't his first language...

Aside from that, you chose to answer when an answer had already been given (and most likely already been marked as the solution). And I think from the tone of your answer that you were just trying to make the point that he didn't phrase his question properly.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900