gpt4 book ai didi

C#如何计算排除特定日期的工作日(节假日)

转载 作者:太空宇宙 更新时间:2023-11-03 18:56:40 25 4
gpt4 key购买 nike

我阅读了一些与C#函数相关的问题/答案来计算工作日(从周一到周五的日子);有些使用扩展代码来实现这一点。

我有一个超过 50,000 行的数据表,我需要一种有效的方法来计算这些信息。

最佳答案

根据@MiBols 的回答,一个更有效的解决方案是使用 HashSet<DateTime>而不是 List<DateTime>然后在 O(1) 中进行搜索:

/// <summary> Get working days between two dates (Excluding a list of dates - Holidays) </summary>
/// <param name="dtmCurrent">Current date time</param>
/// <param name="dtmFinishDate">Finish date time</param>
/// <param name="excludedDates">Collection of dates to exclude (Holidays)</param>
public static int fwGetWorkingDays(this DateTime dtmCurrent, DateTime dtmFinishDate,
HashSet<DateTime> excludedDates)
{
Func<DateTime, bool> workDay = currentDate => (
currentDate.DayOfWeek == DayOfWeek.Saturday ||
currentDate.DayOfWeek == DayOfWeek.Sunday ||
excludedDates.Contains(currentDate.Date));

return Enumerable.Range(0, 1 + (dtmFinishDate - dtmCurrent).Days)
.Count(intDay => workDay(dtmCurrent.AddDays(intDay)));
}

关于C#如何计算排除特定日期的工作日(节假日),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43480593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com