【问题标题】:get monthwise working days between a given start date and end date获取给定开始日期和结束日期之间的每月工作日
【发布时间】:2014-10-16 09:04:46
【问题描述】:

我需要编写一个方法来传递开始日期和结束日期。输出应该是一个包含两个参数的列表。一个是月份名称,另一个是该月的工作日数。 (去掉sat和sun)

请指教。

public List<MonthDaysData> GetMonthwiseWorkingdays(DateTime? start, DateTime? end)
{
List<MonthDaysData> monthdays = new List<MonthDaysData>();

// Coding to get the output
return monthdays;
}

public class MonthDaysData 
{ 
  public Int32? Month { get; set; } 
  public Int32? days { get; set; } 
} 

【问题讨论】:

  • 那么为什么不从开始日期循环到结束日期,检查是否是假期,如果需要,将其添加到列表中?到目前为止,您尝试过什么?
  • 也提供 MonthDaysData 定义。
  • 公共类 MonthDaysData { public Int32?月{得到;放; } 公共 Int32?天{得到;放; } }
  • 嗨,安迪,感谢您的回复。我可以得到两个日期之间的总天数。但我需要的是按月划分工作日。
  • 你试过什么?搜索“C# 获取两个日期之间的工作日”会产生 很多 的结果。如果日期跨度超过一年怎么办?

标签: c# asp.net datetime


【解决方案1】:

您可以使用扩展方法来获取这样的值...

public static class Extensions
{
    public static List<MonthDaysData> GetWorkingDaysPerMonthTo(this DateTime from, 
                         DateTime to)
    {
        var workings = new Dictionary<int, int>();
        var currentDateTime = from;

        while (currentDateTime <= to)
        {
            if (currentDateTime.DayOfWeek != DayOfWeek.Saturday 
                                  && currentDateTime.DayOfWeek != DayOfWeek.Sunday 
                                  && !currentDateTime.IsHoliday("CountryCode"))
                if (!workings.ContainsKey(currentDateTime.Month))
                    workings.Add(currentDateTime.Month, 1);
                else
                {
                    int curWork;
                    workings.TryGetValue(currentDateTime.Month, out curWork);
                    curWork++;
                    workings.Remove(currentDateTime.Month);
                    workings.Add(currentDateTime.Month, curWork);
                }

            currentDateTime = currentDateTime.AddDays(1);
        }
        return workings.Select(work => new MonthDaysData {Month = work.Key, 
                                                       days = work.Value}).ToList();
    } 

    public static bool IsHoliday(this DateTime date, string countryCode)
    {
        // some service that takes a country code and 
        // returns true/false if its a holiday
        return false;
    }
}

然后你可以从任何地方调用它...

var today = new DateTime(2014, 10, 16);
var dates = today.GetWorkingDaysPerMonthTo(new DateTime(2014, 12, 16));

但是,这只是将工作日作为工作日,您需要检查公共假期等。

【讨论】:

    【解决方案2】:

    这听起来像是家庭作业,您没有展示您尝试过的内容,所以我不会为您编写所有代码。关于这件事有quite some questions。例如,请参阅 Get working days DateTime List of two dates 以获取返回工作日日期列表的简单实现:

    IEnumerable<DateTime> workingDays = WorkDaysBetween(DateTime start, DateTime end);
    

    然后您必须根据您的要求按月对它们进行分组:

    var groupedByMonth = workingDays.GroupBy(d => new DateTime(d.Year, d.Month, 1));
    

    从那里你必须能够Select()正确的投影。

    【讨论】:

      猜你喜欢
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多