AddBusinessDays和GetBusinessDays

AddBusinessDays和GetBusinessDays,第1张

AddBusinessDays和GetBusinessDays

您的第一个功能的最新尝试

public static DateTime AddBusinessDays(DateTime date, int days){    if (days < 0)    {        throw new ArgumentException("days cannot be negative", "days");    }    if (days == 0) return date;    if (date.DayOfWeek == DayOfWeek.Saturday)    {        date = date.AddDays(2);        days -= 1;    }    else if (date.DayOfWeek == DayOfWeek.Sunday)    {        date = date.AddDays(1);        days -= 1;    }    date = date.AddDays(days / 5 * 7);    int extraDays = days % 5;    if ((int)date.DayOfWeek + extraDays > 5)    {        extraDays += 2;    }    return date.AddDays(extraDays);}

第二个函数Ge​​tBusinessDays可以实现如下:

public static int GetBusinessDays(DateTime start, DateTime end){    if (start.DayOfWeek == DayOfWeek.Saturday)    {        start = start.AddDays(2);    }    else if (start.DayOfWeek == DayOfWeek.Sunday)    {        start = start.AddDays(1);    }    if (end.DayOfWeek == DayOfWeek.Saturday)    {        end = end.AddDays(-1);    }    else if (end.DayOfWeek == DayOfWeek.Sunday)    {        end = end.AddDays(-2);    }    int diff = (int)end.Subtract(start).TotalDays;    int result = diff / 7 * 5 + diff % 7;    if (end.DayOfWeek < start.DayOfWeek)    {        return result - 2;    }    else{        return result;    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5427608.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存