Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How to calculate total months from start date to end date
i have tried but am getting only total days from startdate to enddate

my code is

DateTime Date1 = Convert.ToDateTime(txtStartDate.Text);
DateTime Date2 = Convert.ToDateTime(txtEndDate.Text);
int DayDiff = (Date2.Date - Date1.Date).Days;
Label1.Text = DayDiff.ToString();
Posted
Comments
Mostafa Asaduzzaman 24-May-15 6:02am    
why not you calculate months from the days?

1 solution

Um...
Months is slightly complex - because it depends on the start and end dates, rather than on a time span between them - the number of days can be the same, but can produce a different "months" count, depending on when the start date was.

Have a look here: Working with Age: it's not the same as a TimeSpan![^] - my Age class will return months for you!
 
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