how to calculate month difference between two days in c#

For example,

2008/01/01 and 2008/05/01 = 4

2008/01/02 and 2008/05/01 = 3

2
avmood
5/16/2008 3:35:04 PM
📁 asp.net.web-forms
📃 93655 articles.
⭐ 5 followers.

💬 3 Replies
👁️‍🗨️ 1300 Views

I got it.

now and past are DateTime varaiables.

int monthsApart = 12 * (now.Year - Past.Year) + now.Month - Past.Month;

if (now.Day < Past.Day)

{

monthsApart--;

}

0
avmood
5/16/2008 3:49:28 PM

You could use timespan

Timespan ts =  Date1 - Date2

 convert the result to months.


Dorm Mom LLC: College Laundry Services | Grocery Delivery | Apartment Cleaning
0
eopeter
5/16/2008 4:07:59 PM

Using Timespan, the end result will be in days, not in months, and I cannot just divide it by 30 to assume that each month is 30 days.

0
avmood
5/16/2008 4:42:25 PM
Reply: