Difference in months between two dates

前端 未结 30 1231
天命终不由人
天命终不由人 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 11:44

    public static int PayableMonthsInDuration(DateTime StartDate, DateTime EndDate)
    {
        int sy = StartDate.Year; int sm = StartDate.Month; int count = 0;
        do
        {
            count++;if ((sy == EndDate.Year) && (sm >= EndDate.Month)) { break; }
            sm++;if (sm == 13) { sm = 1; sy++; }
        } while ((EndDate.Year >= sy) || (EndDate.Month >= sm));
        return (count);
    }
    

    This solution is for Rental/subscription calculation, where difference doesn't means to be subtraction, it's meant to be the span in within those two dates.

提交回复
热议问题