Difference in months between two dates

前端 未结 30 1580
天命终不由人
天命终不由人 2020-11-22 11:02

How to calculate the difference in months between two dates in C#?

Is there is equivalent of VB\'s DateDiff() method in C#. I need to find difference in

30条回答
  •  深忆病人
    2020-11-22 12:02

    To get difference in months (both start and end inclusive), irrespective of dates:

    DateTime start = new DateTime(2013, 1, 1);
    DateTime end = new DateTime(2014, 2, 1);
    var diffMonths = (end.Month + end.Year * 12) - (start.Month + start.Year * 12);
    

提交回复
热议问题