zoukankan      html  css  js  c++  java
  • C# 获取一段日期内的工作日

            /// <summary>
            /// 根据指定时间段计算工作日天数
            /// </summary>
            /// <param name="firstDay"></param>
            /// <param name="lastDay"></param>
            /// <param name="bankHolidays"></param>
            /// <returns></returns>
            public static int BusinessDaysUntil(DateTime firstDay, DateTime lastDay, params DateTime[] bankHolidays)
            {
                firstDay = firstDay.Date;
                lastDay = lastDay.Date;
                if (firstDay > lastDay)
                    throw new ArgumentException("最后一天不正确" + lastDay);
    
                TimeSpan span = lastDay - firstDay;
                int businessDays = span.Days + 1;
                int fullWeekCount = businessDays / 7;
                if (businessDays > fullWeekCount * 7)
                {
                    int firstDayOfWeek = firstDay.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)firstDay.DayOfWeek;
                    int lastDayOfWeek = lastDay.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)lastDay.DayOfWeek;
    
                    if (lastDayOfWeek < firstDayOfWeek)
                        lastDayOfWeek += 7;
                    if (firstDayOfWeek <= 6)
                    {
                        if (lastDayOfWeek >= 7)
                            businessDays -= 2;
                        else if (lastDayOfWeek >= 6)
                            businessDays -= 1;
                    }
                    else if (firstDayOfWeek <= 7 && lastDayOfWeek >= 7)
                        businessDays -= 1;
                }
    
                businessDays -= fullWeekCount + fullWeekCount;
    
                foreach (DateTime bankHoliday in bankHolidays)
                {
                    DateTime bh = bankHoliday.Date;
                    if (firstDay <= bh && bh <= lastDay)
                        --businessDays;
                }
    
                return businessDays;
            }
  • 相关阅读:
    Python基础
    pip install psycopg2出现python setup.py egg_info failed with error code 1 in /tmp/pip-build-YtLeN3/psycopg2错误处理
    Python基础
    C语言基础
    benchmarks
    用 MuGo 搭建 Go Engine 在 KGS 对战
    GPU
    linux 杀掉僵尸进程 (zombie process, defunct)
    CMakeLists.txt 语法
    软件列表(按字母排序)
  • 原文地址:https://www.cnblogs.com/GoCircle/p/8615573.html
Copyright © 2011-2022 走看看