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));
而不是6个月它给了我17个月
答案 0 :(得分: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.