0
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.InvariantCulture;
DateTime dTDoJ = DateTime.ParseExact("07/01/2017", "MM/dd/yyyy", ci);
int months = Math.Abs((DateTime.Now.AddMonths(-1).Month - dTDoJ.Month) + 12 * (DateTime.Now.Year - dTDoJ.Year));

Instead of 6 months it gives me 17 months

rene
  • 41,474
  • 78
  • 114
  • 152

1 Answers1

0

Well, DateTime.Now.Year is 2018, and dToJ.Year is 2017. So the difference is going to be 1 which places the difference to at least 12. The 5 comes from the first term, which is basically 12 - 7, which is 5. Adding them together gets you 17.

If you can get by with Difference in months between two dates for an accurate answer. It's very similar to your formula.

Horia Coman
  • 8,681
  • 2
  • 23
  • 25