Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
how to get datediff between two dates in months in asp.net using C#
Posted 18-Nov-12 19:41pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 310
1 Sergey Alexandrovich Kryukov 166
2 PhilLenoir 164
3 Richard MacCutchan 160
4 Sharmanuj 146
0 Sergey Alexandrovich Kryukov 6,081
1 OriginalGriff 5,115
2 CPallini 2,473
3 Richard MacCutchan 1,597
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100