Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
DateTime dt1 = Calendar2.SelectedDate;
DateTime dt2 = Calendar1.SelectedDate;
TimeSpan ts = dt1 - dt2;
int days = ts.Days;
txtDays.Text = ts.ToString();
Posted
Updated 23-Feb-13 22:47pm
v3
Comments
OriginalGriff 24-Feb-13 4:30am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
Sandeep Mewara 24-Feb-13 4:40am    
|This is not a well framed question! We cannot work out what you are trying to do/ask from the post. Please elaborate and be specific.
Use the "Improve question" link to edit your question and provide better information.
Gilbertinino 24-Feb-13 4:46am    
My question is how can I deduct two dates and I get a result like '4' not '4.00:00:00'
Sandeep Mewara 24-Feb-13 4:48am    
And what if the difference is 0.00:04:53?

You have to share what exactly you are tring to do and whats the end goal expected. difference between two dates will vary from seconds to years. What are you looking for?

1 solution

Ah! That makes more sense.

Don't use the default TimeSpan.ToString: it returns a timespan as days.hours:minutes:seconds
Instead, use a format string to return only the bit you are interested in:
txtDays.Text = ts.ToString("d");
Or use the days value you have already extracted!

http://msdn.microsoft.com/en-gb/library/dd992632.aspx[^]
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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