gpt4 book ai didi

c# - 如何检查当前日期是该月最后一个工作日的第 N 天还是第 N 天?

转载 作者:行者123 更新时间:2023-11-30 22:04:42 24 4
gpt4 key购买 nike

我意识到有人问过类似的问题,但我觉得这个问题有不同的要求。

我需要检查当前日期是该月最后一个工作日的第 N 天还是第 N 天。如果是,我开始另一个任务,否则我退出。

类似的东西:

void doTask()
{
if (NthFirstOrNthLastBusinessDay())
{ // do stuff }
else
{ // do nothing }
}

bool NthFirstOrNthLastBusinessDay()
{}

到目前为止,我想我可以找到答案 here获取该月的最后一个工作日(同样是该月的第一个工作日),然后应用一系列条件来计算从该日期开始的第 N 个工作日。

有没有更好的办法?

例如,如果 N 为 4,则只有当今天是该月的第 4 个工作日或从该月的最后一个工作日算起的第 4 个工作日时,我的任务才应该运行。

本质上,我每天都调用这个方法,它检查当天是否满足这个条件,如果满足则运行任务,否则不运行。

因此,我的任务应该在 2014 年 7 月 28 日和 2014 年 8 月 6 日运行,然后是 2014 年 8 月 26 日,依此类推。

编辑:这是我在 N=4 情况下编写的一些代码:

    /// <summary>
/// Checks if the current day is the 4th business day or the 4th last business day of the month
/// </summary>
/// <returns> True if current day satisfies condition, false otherwise </returns>
private bool NthFirstOrNthLastBusinessDay()
{
return (IsFourthBusinessDay() || IsFourthFromLastBusinessDay());
}

/// <summary>
/// Checks if today is the fourth business day of the month
/// </summary>
/// <returns> True if today satisfies condition, else false </returns>
private bool IsFourthBusinessDay()
{
var dateToday = DateTime.Today;

var firstDayOfThisMonth = new DateTime(dateToday.Year, dateToday.Month, 1);

switch (firstDayOfThisMonth.DayOfWeek)
{
case DayOfWeek.Sunday:
firstDayOfThisMonth = firstDayOfThisMonth.AddDays(4);
break;
case DayOfWeek.Monday:
firstDayOfThisMonth = firstDayOfThisMonth.AddDays(3);
break;
case DayOfWeek.Tuesday:
firstDayOfThisMonth = firstDayOfThisMonth.AddDays(3);
break;
default:
firstDayOfThisMonth = firstDayOfThisMonth.AddDays(5);
break;
} // firstDayOfMonth now contains first business day of month

return dateToday == firstDayOfThisMonth;
}

/// <summary>
/// Checks if today is the fourth from last business day of the month
/// </summary>
/// <returns> True if today satisfies condition, else false </returns>
private bool IsFourthFromLastBusinessDay()
{
var dateToday = DateTime.Today;

var lastDayOfThisMonth = new DateTime(dateToday.Year, dateToday.Month, DateTime.DaysInMonth(dateToday.Year, dateToday.Month));

switch (lastDayOfThisMonth.DayOfWeek)
{
case DayOfWeek.Saturday:
lastDayOfThisMonth = lastDayOfThisMonth.AddDays(-4);
break;
case DayOfWeek.Friday:
lastDayOfThisMonth = lastDayOfThisMonth.AddDays(-3);
break;
case DayOfWeek.Thursday:
lastDayOfThisMonth = lastDayOfThisMonth.AddDays(-3);
break;
default:
lastDayOfThisMonth = lastDayOfThisMonth.AddDays(-5);
break;
} // firstDayOfMonth now contains first business day of month

return dateToday == lastDayOfThisMonth;
}

现在我需要调整一般 N 的值。有更好的方法吗?

最佳答案

根据 OP 对我原始答案的评论,我决定编写另一个函数作为练习。

在这里(再次 - 未完全测试):

// Nth day should not be larger than 14 - I have tested it
bool Nth_BusinessDay(int NthDay, DateTime current)
{
// check we have NthDay larger than ZERO
if (NthDay < 1)
return false;

// make sure the taget date is just a date - no time component
DateTime target = current.Date;

// make sure the specialDay is not larger than the current days in the month
if (DateTime.DaysInMonth(target.Year, target.Month) > NthDay)
return false;

// start at the beginning
DateTime NthStart = new DateTime(target.Year, target.Month, 1);
// check if start date falls on the weekend
if (NthStart.DayOfWeek == DayOfWeek.Saturday)
NthStart = NthStart.AddDays(2); // jump TWO days to Monday
if (NthStart.DayOfWeek == DayOfWeek.Sunday)
NthStart = NthStart.AddDays(1); // jump ONE day to Monday

// now add our Nth day
NthStart = NthStart.AddDays(NthDay - 1);

// if we land on weekend after we adjusted for the Nth - push it by two days
if (NthStart.DayOfWeek == DayOfWeek.Saturday || NthStart.DayOfWeek == DayOfWeek.Sunday)
NthStart = NthStart.AddDays(2);

// start at the end and minus the Nth day
DateTime NthEnd = new DateTime(target.Year, target.Month, DateTime.DaysInMonth(target.Year, target.Month));
// check if the end date falls on the weekend - the difference here is the adjustment
if (NthEnd.DayOfWeek == DayOfWeek.Saturday)
NthEnd = NthEnd.AddDays(-1); // jump ONE day back to Friday
if (NthEnd.DayOfWeek == DayOfWeek.Sunday)
NthEnd = NthEnd.AddDays(-2); // jump TWO days back to Friday

// now subject our Nth day from the End
NthEnd = NthEnd.AddDays((NthDay - 1) * -1);

// again if we land on weekend - adjust by two days
if (NthStart.DayOfWeek == DayOfWeek.Saturday || NthStart.DayOfWeek == DayOfWeek.Sunday)
NthStart = NthStart.AddDays(2);


// check if our target day is one of the Nth day
if (NthStart == target || NthEnd == target)
return true;

return false;
}

并使用上面的函数:

void doTaskNthDay()
{
if (Nth_BusinessDay(5, DateTime.Now))
{
// do stuff
}
}

以下是我留在此处的原始答案,仅供引用。

我从以下代码中获取了以下代码:.NET Date Compare: Count the amount of working days since a date? .我还没有测试过这段代码,但你可以尝试这样的事情:

int BuinessDayCount(DateTime targetDay)
{
// check if the target day is a weekend
if (targetDay.DayOfWeek == DayOfWeek.Saturday)
return -1;
if (targetDay.DayOfWeek == DayOfWeek.Sunday)
return -2;

// get the start of the month
DateTime start = new DateTime(targetDay.Year, targetDay.Month, 1);

// create a target without any time component
DateTime end = targetDay.Date;

int workingDays = 0;
while (start <= targetDay)
{
if (start.DayOfWeek != DayOfWeek.Saturday
&& start.DayOfWeek != DayOfWeek.Sunday)
{
workingDays++;
}
start = start.AddDays(1);
}

return workingDays;
}

然后在您的代码中我会执行以下操作:

void doTask()
{
int businessDay = BuinessDayCount(DateTime.Now);
if (businessDay == 5 || businessDay == 25)
{
// do stuff
}
}

我认为它会读起来更好。

关于c# - 如何检查当前日期是该月最后一个工作日的第 N 天还是第 N 天?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25000360/

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