0

我需要确定接下来的两个连续工作日是否是假期(美国工作日),不包括周末。

我为解决这个问题所做的努力:

todayVariable = Date.Now()

取这个变量的日期部分,如果是 1 或 7,那意味着它是一个周末,所以我将不理会它。但是,我不知道如何确定工作日是否不是假期。例如,我如何获得未来两年所有法定假日的列表?

4

3 回答 3

3

如果明天是假期,用这个来回答:

Dim todayVariable As Date = Today
Debug.Print(isHoliday(todayVariable.AddDays(1)))

Function isHoliday(ByVal dt As Date)
    Return getHolidayList(dt.Year).Any(Function(x) x = dt)
End Function

下面的代码将计算美国联邦假期的日期。这种方法的一个缺点是,由于规则是硬编码的,我需要在国会更改规则的罕见事件中更改代码。对于我的内部软件来说这不是问题,但对于其他人来说可能是这样。

此外,我不计算复活节,因为那不是美国联邦假日。参见Nature (1876) 计算复活节日期的算法

Public Function getHolidayList(ByVal vYear As Integer) As List(Of Date)

    Dim FirstWeek As Integer = 1
    Dim SecondWeek As Integer = 2
    Dim ThirdWeek As Integer = 3
    Dim FourthWeek As Integer = 4
    Dim LastWeek As Integer = 5

    Dim HolidayList As New List(Of Date)

    '   http://www.usa.gov/citizens/holidays.shtml      
    '   http://archive.opm.gov/operating_status_schedules/fedhol/2013.asp

    ' New Year's Day            Jan 1
    HolidayList.Add(DateSerial(vYear, 1, 1))

    ' Martin Luther King, Jr. third Mon in Jan
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 1, 1), DayOfWeek.Monday, ThirdWeek))

    ' Washington's Birthday third Mon in Feb
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 2, 1), DayOfWeek.Monday, ThirdWeek))

    ' Memorial Day          last Mon in May
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 5, 1), DayOfWeek.Monday, LastWeek))

    ' Independence Day      July 4
    HolidayList.Add(DateSerial(vYear, 7, 4))

    ' Labor Day             first Mon in Sept
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 9, 1), DayOfWeek.Monday, FirstWeek))

    ' Columbus Day          second Mon in Oct
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 10, 1), DayOfWeek.Monday, SecondWeek))

    ' Veterans Day          Nov 11
    HolidayList.Add(DateSerial(vYear, 11, 11))

    ' Thanksgiving Day      fourth Thur in Nov
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 11, 1), DayOfWeek.Thursday, FourthWeek))

    ' Christmas Day         Dec 25
    HolidayList.Add(DateSerial(vYear, 12, 25))

    'saturday holidays are moved to Fri; Sun to Mon
    For i As Integer = 0 To HolidayList.Count - 1
        Dim dt As Date = HolidayList(i)
        If dt.DayOfWeek = DayOfWeek.Saturday Then
            HolidayList(i) = dt.AddDays(-1)
        End If
        If dt.DayOfWeek = DayOfWeek.Sunday Then
            HolidayList(i) = dt.AddDays(1)
        End If
    Next

    'return
    Return HolidayList

End Function

Private Function GetNthDayOfNthWeek(ByVal dt As Date, ByVal DayofWeek As Integer, ByVal WhichWeek As Integer) As Date
    'specify which day of which week of a month and this function will get the date
    'this function uses the month and year of the date provided

    'get first day of the given date
    Dim dtFirst As Date = DateSerial(dt.Year, dt.Month, 1)

    'get first DayOfWeek of the month
    Dim dtRet As Date = dtFirst.AddDays(6 - dtFirst.AddDays(-(DayofWeek + 1)).DayOfWeek)

    'get which week
    dtRet = dtRet.AddDays((WhichWeek - 1) * 7)

    'if day is past end of month then adjust backwards a week
    If dtRet >= dtFirst.AddMonths(1) Then
        dtRet = dtRet.AddDays(-7)
    End If

    'return
    Return dtRet

End Function
于 2014-11-14T04:58:07.340 回答
0

没有一个普遍认可的美国官方假期清单。最接近的可能是联邦假日日历,但其中包括大多数企业保持营业的日子,例如华盛顿生日和哥伦布日。

许多企业选择将他们的假期放在数据库表中或合并到日历表中。

于 2012-10-12T20:49:32.900 回答
0

正如其他人已经指出的那样,似乎没有可以“计算”或简单地“到达某个地方”的假期列表。您必须对适用的日期列表进行硬编码。

关于您的描述的一些评论,以帮助您避免任何其他问题:

Date.Now 给出“现在”的日期和时间。如果您想在不考虑时间的情况下比较日期,您可以使用 Date.Today。

dateValue.DatePart 确实为您提供了日期部分(年、月、日)。您可能想知道星期几(星期日,星期一,...)。这是这样的:

  Dim dayValue As System.DayOfWeek = dateValue.DayOfWeek

结果是一个整数 Enum,其值为 Sunday(=0)、Monday(=1) 等等。

为避免混淆:dateValue (dateValue.Day) 的 Day 属性为您提供一个月中的天数 (1..31)。

于 2012-10-13T10:29:16.360 回答