Click here to Skip to main content
15,917,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get the remaining no of days from given a date to that end of year
Posted
Updated 21-Jul-11 4:43am
v2

From a given date to when?

Use DateTime objects and subtract them. That gives you a TimeSpan object which has a TotalDays property:

double days = ((TimeSpan)date1-date2).TotalDays;
 
Share this answer
 
v2
Comments
walterhevedeich 21-Jul-11 10:47am    
Correct. 5ed
Sergey Alexandrovich Kryukov 22-Jul-11 23:24pm    
Not 100% correct, please see my fix.
--SA
A fix to the answer by Wjousts: the type cast to TimeSpan it incorrect (just to date1?) and redundant, as subtraction operand for two instances of System.DateTime returns System.TimeSpan, so correct expression is:

C#
double days = (date1-date2).TotalDays;


See http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

—SA
 
Share this answer
 
Comments
Mohammad A Rahman 23-Jul-11 0:40am    
5!,
As always. Nice. :)
Sergey Alexandrovich Kryukov 23-Jul-11 0:53am    
Thank you, Mohammad.
--SA

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