我需要编写一个方法,在其中我将传递开始日期和结束日期。输出应该是带有两个参数的列表。一个是月份名称,另一个是该月的工作日数。(去除坐姿和太阳)
请指教。
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; }
}
您可以使用扩展方法来获取像这样的值...
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));
但是,这只是将工作日作为工作日,您需要检查公共假期等。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句