Difference in months between two dates

前端 未结 30 1575
天命终不由人
天命终不由人 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条回答
  •  Happy的楠姐
    2020-11-22 11:44

    Use Noda Time:

    LocalDate start = new LocalDate(2013, 1, 5);
    LocalDate end = new LocalDate(2014, 6, 1);
    Period period = Period.Between(start, end, PeriodUnits.Months);
    Console.WriteLine(period.Months); // 16
    

    (example source)

提交回复
热议问题