【问题标题】:Generate a list of weekly business dates between two dates excluding weekends and holidays生成两个日期之间的每周营业日期列表,不包括周末和节假日
【发布时间】:2019-02-22 19:54:58
【问题描述】:

我想生成两个日期之间的每周营业日期列表,不包括周末和节假日。我设法排除了周末并为假期制定了例行公事。现在我需要排除假期。下面是我的代码。

using System;
using System.Collections.Generic;
using System.Linq;

namespace SeriesTest
{
    class Program
    {
        public class BusinessWeekDays
        {
            public DateTime Monday;
            public DateTime Sunday;
        }

        private static List<DateTime> Holidays = new List<DateTime>()
        {
            new DateTime(1, 1, 1), //New Year Day
            new DateTime(1, 5, 1), //Labour Day
            new DateTime(1, 7, 4), //Independence Day
            new DateTime(1, 3, 1), //Martin Luther King Jr. Day
            new DateTime(1, 3, 2), //Presidents Day 
            new DateTime(1, 12, 25), //Christmas
            new DateTime(1, 5, 5), //Memorial Day
            new DateTime(1, 9, 1), //Labor Day
            new DateTime(1, 10, 2), //Columbus Day
            new DateTime(1, 11, 4), //Columbus Day
        };

    private static bool IsHoliday(DateTime value, List<DateTime> holidays = null)
    {
        if (null == holidays)
            holidays = Holidays;

        return (value.DayOfWeek == DayOfWeek.Sunday) ||
               (value.DayOfWeek == DayOfWeek.Saturday) ||
               holidays.Any(holiday => holiday.Day == value.Day &&
                                       holiday.Month == value.Month);
    }

    public static int BusinessDays(DateTime fromDate, DateTime toDate, List<DateTime> holidays = null)
    {
        int result = 0;
        for (var date = fromDate;
            date < toDate.Date;
            date = date.AddDays(1))
            if (!IsHoliday(date, holidays))
                result += 1;

        return result;
    }

    static void Main(string[] args)
    {


        var StartDate = DateTime.Parse("02/12/2019");
        var SeriesEndDate = DateTime.Parse("12/31/2025");
        var holidays = new List<DateTime>();

        var firstMonday = Enumerable.Range(0, 7)
            .SkipWhile(x => StartDate.AddDays(x).DayOfWeek != DayOfWeek.Monday)
            .Select(x => StartDate.AddDays(x))
            .First();

        var ts = (SeriesEndDate - firstMonday);

        var dates = new List<BusinessWeekDays>();

        for (var i = 0; i < ts.Days; i += 7)
        {

            //Remove holidays. Weekend already removed here
            if (BusinessDays(StartDate, SeriesEndDate, holidays) != 0)
            {

                dates.Add(new BusinessWeekDays { Monday = firstMonday.AddDays(i), Sunday = firstMonday.AddDays(i + 9) });
            }

        }

        Console.WriteLine(dates);

        }


    }
}

【问题讨论】:

    标签: c#


    【解决方案1】:

    您似乎已经拥有所需的一切,但holidays 从未设置为您的Holidays。更改下面的行,以便将 holidays 设置为 null。然后它将在IsHoliday 中进行空检查并正确设置。

    var holidays = new List<DateTime>();
    

    应该是:

    var holidays = null;
    

    【讨论】:

      猜你喜欢
      • 2019-07-14
      • 2019-10-03
      • 2012-08-22
      • 2020-12-08
      • 2015-04-25
      • 1970-01-01
      • 2023-03-26
      • 2018-04-04
      • 1970-01-01
      相关资源
      最近更新 更多