2012-10-12 58 views
0

我需要確定下兩個連續工作日是否爲節假日(美國工作日),不包括週末。如何確定日期是否爲假期

我的努力來解決這個問題:

todayVariable = Date.Now() 

考慮此變量的日期部分,如果是1或7,這意味着它的一個週末,所以我會獨自離開它。但是,我不知道如何確定星期幾是不是假期。例如,如何獲得未來兩年所有官方假期的清單?

+0

用什麼語言? – assylias

+0

任何語言最好vb.net或sql查詢 – user1733138

+2

你打算如何查找假期是什麼?如果您有某種列表假期,您可以檢查下兩天是否爲假期。這篇文章在這方面可能會有所幫助。 http://stackoverflow.com/questions/457176/how-do-i-determine-a-public-holiday-in-sql-server – Corey

回答

-1

美國官方假期中沒有普遍認同的清單。可能最接近的是federal holiday日曆,但包括大多數企業仍然開放的日子,例如華盛頓的生日和哥倫布日。

許多企業選擇將他們的假期放在數據庫表中或合併到日曆表中。

+0

是的我有一張表,其中consis字段holidayID,holidayDate,日期標題; 95 2011-04-22良好星期五(一條記錄) – user1733138

+0

您確定「沒有共同商定的官方美國節日名單」嗎?我無法相信。 –

+0

有一個聯邦假期的列表,但許多企業沒有觀察到這些假期:2月的第三個星期一慶祝華盛頓的生日,USMail不會交付,但我會和大多數客戶一樣在工作。聯邦快遞和UPS將照常運作。 因此,隨着OP的要求,我不知道你如何確定任何企業的工作日而不知道他們的政策。 –

0

正如其他人已經指出,似乎沒有一個假期的列表,你可以「計算」或只是「得到某處」。您必須對適用的日期列表進行硬編碼。

您的描述一些言論,可以讓你避免任何其他問題:

Date.Now給出了日期和「現在」的時間。如果你想比較日期而不考慮時間,你可以使用Date.Today。

dateValue.DatePart實際上給你日期部分(年,月,日)。你大概想知道星期幾(星期日,星期一,......)。如下所示:

Dim dayValue As System.DayOfWeek = dateValue.DayOfWeek 

結果是一個整數枚舉,其值爲星期日(= 0),星期一(= 1)等。

爲了避免混淆:dateValue(dateValue.Day)的Day屬性爲您提供了一個月中的日期編號(1..31)。

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 
相關問題