当前位置: 首页>>代码示例>>C#>>正文


C# DayOfWeek.Last方法代码示例

本文整理汇总了C#中DayOfWeek.Last方法的典型用法代码示例。如果您正苦于以下问题:C# DayOfWeek.Last方法的具体用法?C# DayOfWeek.Last怎么用?C# DayOfWeek.Last使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DayOfWeek的用法示例。


在下文中一共展示了DayOfWeek.Last方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: BusinessDays

        /// <summary>
        ///     Determines the number of business days between two dates
        /// </summary>
        /// <param name="start">The DateTime to use as the start when determining the difference</param>
        /// <param name="end">The DateTime to use as the end when determining the difference</param>
        /// <param name="weekendDays">
        ///     The days of the week to consider as the weekend. These days of the week will be excluded when
        ///     determining business days.
        /// </param>
        /// <param name="holidays">Holidays dates to exclude when determining business days</param>
        /// <returns>The number of business days between the <paramref name="start" /> and <paramref name="end" /> dates</returns>
        public static int BusinessDays(DateTime start, DateTime end, DayOfWeek[] weekendDays, params DateTime[] holidays)
        {
            start = start.Date;
            end = end.Date;

            if (start > end)
                throw new ArgumentException($"End date must be greater than {start:F}", nameof(end));

            if (weekendDays == null || !weekendDays.Any())
                throw new ArgumentException("Weekend Days is null or empty");

            var lastWeekendDay = weekendDays.Last();
            var span = end - start;
            var businessDays = span.Days + 1;
            var fullWeekCount = businessDays/7;

            if (businessDays > fullWeekCount*7)
            {
                var startDayOfWeek = start.DayOfWeek == lastWeekendDay
                                         ? 7
                                         : (int)start.DayOfWeek;
                var endDayOfWeek = end.DayOfWeek == lastWeekendDay
                                       ? 7
                                       : (int)end.DayOfWeek;

                if (endDayOfWeek < startDayOfWeek)
                    endDayOfWeek += 7;

                if (startDayOfWeek <= 6)
                {
                    if (endDayOfWeek >= 7)
                        businessDays -= 2;
                    else if (endDayOfWeek >= 6)
                        businessDays -= 1;
                }
                else if (startDayOfWeek <= 7 && endDayOfWeek >= 7)
                    businessDays -= 1;
            }

            businessDays -= (from holiday in holidays
                             where holiday.Date >= start && holiday.Date <= end && weekendDays.Any(weekendDay => weekendDay != holiday.DayOfWeek)
                             select holiday).Count() + 2*fullWeekCount;

            return businessDays;
        }
开发者ID:KatoTek,项目名称:Encompass,代码行数:56,代码来源:Date.cs


注:本文中的DayOfWeek.Last方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。