C# 获取给定开始日期和结束日期之间的每月工作日
我需要编写一个方法,在其中传递开始日期和结束日期。输出应该是一个带有两个参数的列表。一个是月名,另一个是该月的工作日数。移除卫星和太阳 请告知C# 获取给定开始日期和结束日期之间的每月工作日,c#,asp.net,datetime,C#,Asp.net,Datetime,我需要编写一个方法,在其中传递开始日期和结束日期。输出应该是一个带有两个参数的列表。一个是月名,另一个是该月的工作日数。移除卫星和太阳 请告知 public List<MonthDaysData> GetMonthwiseWorkingdays(DateTime? start, DateTime? end) { List<MonthDaysData> monthdays = new List<MonthDaysData>(); // Coding to ge
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; }
}
这听起来像是家庭作业,你没有展示你已经尝试过的东西,所以我不会为你编写所有的代码。在这件事上有许多不同的看法。有关返回工作日日期列表的简单实现,请参见示例:
IEnumerable<DateTime> workingDays = WorkDaysBetween(DateTime start, DateTime end);
从这里,您必须能够选择正确的投影。您可以使用扩展方法获得如下值
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;
}
}
不过,这只是将工作日作为工作日,您需要检查公共假日等。所以为什么不从开始日期循环到结束日期,检查是否为假日,如果必要,将其添加到列表中?到目前为止,您尝试了什么?也提供MonthDaysData定义。公共类MonthDaysData{public Int32?Month{get;set;}public Int32?days{get;set;}}嗨,andy,谢谢您的回复。。我可以得到两个日期之间的总天数。但我需要的是每月一次的工作日分割。你试过什么?搜索两个日期之间的C Get工作日会产生很多结果。如果日期跨度超过一年,该怎么办?
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));