Click here to Skip to main content
15,886,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to get datediff between two dates in months in asp.net using C#
Posted

 
Share this answer
 
v2
You can try this simple method:

DateTime Date1=new DateTime(); DateTime Date2=new DateTime();

int Value = (Date1.Year * 12 + Date1.Month) - (Date2.Year * 12 + Date2.Month);
 
Share this answer
 
 
Share this answer
 
I think this is an incomplete question. You should give an example of two dates and tell how you want the output.

Because, its not clear if it is 15 days of difference between two dates, How do you want the output? 0 months ? or 0.5 months? If you want 0.5 months then what if the month is February? And if it leap-year !

If you want difference in terms of 0 years, 0 months, 15 days kind then you have to specify accordingly.
C#
DateTime date1 = new DateTime(2009, 8, 1);
DateTime date2 = new DateTime(2009, 9, 5);                      
            
int Days  = date2.Year - date1.Year;
int Months = date2.Month - date1.Month;
int Years = date2.Day - date1.Day;

This will give the output as I explained.

If you want just days then what _TR_ gave links will work i.e. subtract and then gets days and divide the output by 30 !

Hope this helps.
Thanks
Milind
 
Share this answer
 

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