如何检查当天是从当月的最后一个工作日起的第N个还是第N个?

时间:2014-07-28 17:01:39

标签: c# .net

我意识到有类似的问题已被问到,但我觉得这些问题有不同的要求。

我需要检查当天是否是该月最后一个工作日的第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的值。有更好的方法吗?

3 个答案:

答案 0 :(得分:3)

根据对原始答案的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
    }
}

我认为它会更好阅读。

答案 1 :(得分:0)

我会做类似的事情:

int numOfOccurredBusinessDays = 0; // this will include current business day
int businessDaysLeft = 0;
var currentDate = DateTime.Now;
var monthIterator = new DateTime(DateTime.Year, DateTime.Month, 1);

for (int i = 0; i < DateTime.DaysInMonth; ++i, monthIterator.AddDay(1))
{
    if (monthIterator <= currentDate &&
     !monthIterator.Date.ToString("D").Contains("Sat") &&
     !monthIterator.Date.ToString("D").Contains("Sun"))
          numOfOccurredBusinessDays++;
    else if (monthIterator > currentDate &&
     !monthIterator.Date.ToString("D").Contains("Sat") &&
     !monthIterator.Date.ToString("D").Contains("Sun"))
          businessDaysLeft++;
}

答案 2 :(得分:-1)

  1. 创建一个新的System.DateTime对象。使用当前年份和当前月份(可以从DateTime.Now获取)对其进行初始化。将日期设置为30(如果是9月/ 4月/ 6月/ 11月,或31,28等)

  2. 现在您有一个DateTime对象代表该月的最后一天。您可以使用DateTime.DayOfWeek来检查日期是星期六还是星期日(或假日)。

  3. 您可以使用DateTime.AddDays(-N)将DateTime对象“回退”一天,然后再次检查它是星期六或星期日(或假日)。

  4. 重复本月最后一天的任何一天检查你需要做的事。