如何确定日期是否为假日

时间:2012-10-12 18:58:24

标签: sql vb.net date

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

我努力解决这个问题:

todayVariable = Date.Now()

取这个变量的日期部分,如果它是1或7,这意味着它是一个周末,所以我将不管它。但是,我不知道如何确定工作日是否不是假期。我怎样才能得到一份例如未来两年所有法定假日的清单?

3 个答案:

答案 0 :(得分:0)

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

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

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

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

  Dim dayValue As System.DayOfWeek = dateValue.DayOfWeek

结果是一个整数枚举,其值为星期日(= 0),星期一(= 1)等。

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

答案 1 :(得分:0)

如果明天是假期,请使用此答案:

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) Algorithm for Calculating the Date of Easter

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

答案 2 :(得分:-1)

没有一个共同商定的美国官方假期清单。可能最接近的是federal holiday日历,但这包括大多数企业保持开放的日子,例如华盛顿的生日和哥伦布日。

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