gpt4 book ai didi

c# - 从今天开始查找接下来的 5 个工作日

转载 作者:行者123 更新时间:2023-12-03 13:46:54 24 4
gpt4 key购买 nike

我用 nager.date知道一天是假日还是周末星期六和星期日)。
我需要在 5 个工作日后提取日期(从今天或任何其他日期开始)。

DateTime date1 = new DateTime(2019, 12, 23); 
int i = 0;

while ( i < 5)
{
if (DateSystem.IsPublicHoliday(date1, CountryCode.IT) || DateSystem.IsWeekend(date1, CountryCode.IT))
{
date1 = date1.AddDays(1);
}
else
{
date1= date1.AddDays(1);
i++;
}
}
这段代码的问题是,如果最后一个 else 发生,它会添加我 1 天,但不做任何其他检查。

For example:
If the start date is 13/07/2020, the result will be at the end 18/07/2020 and as you can see is on Saturday.


我怎样才能修改这段代码来实现我需要的?

最佳答案

顺序很重要。 AddDays应该首先被调用,在调用之后我们检查新的一天是否符合我们的标准。
备注 : 我已经改名为 i变量,所以更清楚。

DateTime date1 = new DateTime(2019, 12, 23); 
int daysAdded = 0;

while (daysAdded < 5)
{
date1 = date1.AddDays(1);
if (!DateSystem.IsPublicHoliday(date1, CountryCode.IT) && !DateSystem.IsWeekend(date1, CountryCode.IT)) {
// We only consider laboral days
// laboral days: They are not holidays and are not weekends
daysAdded ++;
}
}

关于c# - 从今天开始查找接下来的 5 个工作日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62931868/

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