小编San*_*ana的帖子

如何获得两个日期之间的月份集合?

以下是我的代码.我只是得到两个日期之间的差异,但我想要那个月的名字来自于和来日期.

public static int GetMonthsBetween(DateTime from, DateTime to)
{
    if (from > to) return GetMonthsBetween(to, from);

    var monthDiff = Math.Abs((to.Year * 12 + (to.Month - 1)) - (from.Year * 12 + (from.Month - 1)));

    if (from.AddMonths(monthDiff) > to || to.Day < from.Day)
    {
        return monthDiff - 1;
    }
    else
    {
        return monthDiff;
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# datetime

3
推荐指数
1
解决办法
3791
查看次数

标签 统计

.net ×1

c# ×1

datetime ×1