Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i did an operation between two DateTimePickers to get number of days JUST DAYS (no months no time), but i face some troubles.
i did this code, it works but just through the period of year(2011), when the date begin in flowing year(2012) ,

for example : 01/01/2013 - 31/12/2012 = -365

TbnbJourLocation.Text =	(DtpDateRetour.Value.DayOfYear -              DtpDateLocation.Value.DayOfYear ).ToString();

and i tried this but it gives me not just number of days, but it's gives me days , time, month...

TbnbJourLocation.Text = (DtpDateRetour.Value - DtpDateLocation.Value ).ToString();

so please help me it's very important
Posted

What you should use is the Timespan Structure[^].

You would use something like this:
C#
Timespan result = DtpDateRetour.Value - DtpDateLocation.Value;
TbnbJourLocation.Text = Convert.ToInt32( result.TotalDays + 0.5 ).ToString();
 
Share this answer
 
Comments
Atif BOUZAGLAOUI 19-Jan-12 11:43am    
thank you so much
Sergey Alexandrovich Kryukov 19-Jan-12 17:41pm    
Sure, a 5.
--SA
Atif BOUZAGLAOUI 19-Jan-12 21:33pm    
for what should we use 0.5 ???
thanks Marcus
This solution is an additional answer to Marcus's answer.

Here is another way that you can apply:

C#
TimeSpan dateDifference = new TimeSpan();
DateTime beginDate = DtpDateRetour.SelectedDate;
DateTime endDate = DtpDateLocation.SelectedDate;
dateDifference = beginDate.Subtract(endDate);


Good luck,
OI
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Jan-12 17:43pm    
I would also write subtraction operator "-" instead "Subtract", would be much more readable. OP also probably need a reference to format strings to format TimeSpan value as a string.
--SA
Atif BOUZAGLAOUI 19-Jan-12 20:47pm    
thank you
You can get days difference between two dates using TimeSpan
TimeSpan ts = DtpDateRetour.Value.Subtract(DtpDateLocation.Value);
TbnbJourLocation.Text = ts.Days.ToString();
 
Share this answer
 
Comments
Atif BOUZAGLAOUI 20-Jan-12 9:25am    
thanks
Dear Atif;
date - the date for the other function, or more exactly, the difference operators var.Bunlardan Time Span, most importantly providing solutions işlevidir.bunu review.
 
Share this answer
 
Comments
Orcun Iyigun 19-Jan-12 11:45am    
:)) a weird answer but love the effort..

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