Tutorial :calculate period between two monthcalender in C#



Question:

I have two monthcalender in C# win application , and I need to calculate the period between then.

I need how many day between two or three month or also years

I need how many month between tow different years.

When I use :

monthcalender.selectstart.month;

this command just calculate the different between months in same year, but when move to next year the value be negative.

and same for days, I use :

monthcalender.selectstart.dayofyear;


Solution:1

monthcalendar.SelectionStart is a DateTime structure, which you can do calculations with. Subtracting two dates will result in a TimeSpan structure, which has various properties that should be of use to you.

TimeSpan timeBetween = calendar1.SelectionStart - calendar2.SelectionStart;  MessageBox.Show("Days between dates: " + timeBetween.TotalDays);  

If you wanted to use the Month property of the DateTime, you could do something like:

DateTime d1 = calendar1.SelectionStart;  DateTime d2 = calendar2.SelectionStart;  ints monthsBetween = d1.Month + d1.Year * 12 - d2.Month - d2.Year * 12;  

That would leave the days of the month out of the equation though.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »