Vb.net 如何计算美国的假期

Vb.net 如何计算美国的假期,vb.net,Vb.net,我需要知道如何计算美国假期。我需要一个适用于任何年份的解决方案。我不想简单地将日期存储在需要维护的数据库中 对于周末的假期,它需要遵循美国政府的政策,以适应工作日。如果是周六,则调整为周五。如果是星期天,则需要调整为星期一。我知道美国很多(大多数?)银行都是这样做的 如何计算美国假期列表 Public Function getHolidayList(ByVal vYear As Integer) As List(Of Date) Dim HolidayList As New List(

我需要知道如何计算美国假期。我需要一个适用于任何年份的解决方案。我不想简单地将日期存储在需要维护的数据库中

对于周末的假期,它需要遵循美国政府的政策,以适应工作日。如果是周六,则调整为周五。如果是星期天,则需要调整为星期一。我知道美国很多(大多数?)银行都是这样做的

如何计算美国假期列表

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

    Dim HolidayList As New List(Of Date)

    '...fill the list with holidays 
    ' New Year's Day            Jan 1
    ' Martin Luther King, Jr. third Mon in Jan
    ' Washington's Birthday third Mon in Feb
    ' Memorial Day          last Mon in May
    ' Independence Day      July 4
    ' Labor Day             first Mon in Sept
    ' Columbus Day          second Mon in Oct
    ' Veterans Day          Nov 11
    ' Thanksgiving Day      fourth Thur in Nov
    ' Christmas Day         Dec 25

    'adjust for weekends

End Function


这是一种方法。这种方法的一个缺点是,由于规则是硬编码的,我需要在国会更改规则的罕见情况下更改代码。对于我的内部软件来说,这不是问题,但对其他人来说可能是问题

我也不计算复活节,因为那不是美国的联邦假日。看


CodeProject上的Jay Muntz使用XML文件存储规则,实现了出色的假日。不过,该代码不适用于周末

此外,他对税收日(4月15日)的说法并不适用于华盛顿特区的解放日,尽管这可能与这个问题无关

转换为C#作为控制台程序。。。我需要在文本文件中快速输入日期:

class Program
{
    public class Holiday
    {
        public string HolidayName { get; set; }
        public DateTime Date { get; set; }

        public Holiday(string holidayName, DateTime date)
        {
            HolidayName = holidayName;
            Date = date;
        }
    }

    public static List<Holiday> getHolidayList(int vYear)
    {
        int FirstWeek = 1;
        int SecondWeek = 2;
        int ThirdWeek = 3;
        int FourthWeek = 4;
        int LastWeek = 5;

        List<Holiday> HolidayList = new List<Holiday>();

        //   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(new Holiday("NewYears", new DateTime(vYear, 1, 1)));

        // Martin Luther King, Jr. third Mon in Jan
        HolidayList.Add(new Holiday("MLK", GetNthDayOfNthWeek(new DateTime(vYear, 1, 1), DayOfWeek.Monday, ThirdWeek)));

        // Washington's Birthday third Mon in Feb
        HolidayList.Add(new Holiday("WashingtonsBDay", GetNthDayOfNthWeek(new DateTime(vYear, 2, 1), DayOfWeek.Monday, ThirdWeek)));

        // Memorial Day          last Mon in May
        HolidayList.Add(new Holiday("MemorialDay", GetNthDayOfNthWeek(new DateTime(vYear, 5, 1), DayOfWeek.Monday, LastWeek)));

        // Independence Day      July 4
        HolidayList.Add(new Holiday("IndependenceDay", new DateTime(vYear, 7, 4)));

        // Labor Day             first Mon in Sept
        HolidayList.Add(new Holiday("LaborDay", GetNthDayOfNthWeek(new DateTime(vYear, 9, 1), DayOfWeek.Monday, FirstWeek)));

        // Columbus Day          second Mon in Oct
        HolidayList.Add(new Holiday("Columbus", GetNthDayOfNthWeek(new DateTime(vYear, 10, 1), DayOfWeek.Monday, SecondWeek)));

        // Veterans Day          Nov 11
        HolidayList.Add(new Holiday("Veterans", new DateTime(vYear, 11, 11)));

        // Thanksgiving Day      fourth Thur in Nov
        HolidayList.Add(new Holiday("Thanksgiving", GetNthDayOfNthWeek(new DateTime(vYear, 11, 1), DayOfWeek.Thursday, FourthWeek)));

        // Christmas Day         Dec 25
        HolidayList.Add(new Holiday("Christmas", new DateTime(vYear, 12, 25)));

        //saturday holidays are moved to Fri; Sun to Mon
        foreach (var holiday in HolidayList)
        {
            if (holiday.Date.DayOfWeek == DayOfWeek.Saturday)
            {
                holiday.Date = holiday.Date.AddDays(-1);
            }
            if (holiday.Date.DayOfWeek == DayOfWeek.Sunday)
            {
                holiday.Date = holiday.Date.AddDays(1);
            }
        }

        //return
        return HolidayList;

    }

    private static System.DateTime GetNthDayOfNthWeek(DateTime dt, DayOfWeek dayofWeek, int WhichWeek)
    {
        //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
        System.DateTime dtFirst = new DateTime(dt.Year, dt.Month, 1);

        //get first DayOfWeek of the month
        System.DateTime dtRet = dtFirst.AddDays(6 - (int)dtFirst.AddDays(-1 * ((int)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))
        {
            dtRet = dtRet.AddDays(-7);
        }

        //return
        return dtRet;

    }

    static void Main(string[] args)
    {
        for(int ii = 2013; ii < 2100; ii++)
        {
            var holidays = getHolidayList(ii);

            foreach (var holiday in holidays)
            {
                Console.WriteLine(holiday.HolidayName + ": " + holiday.Date.ToShortDateString());
            }
        }
    }
}
类程序
{
公休假日
{
公共字符串HolidayName{get;set;}
公共日期时间日期{get;set;}
公共假日(字符串holidayName、DateTime日期)
{
HolidayName=HolidayName;
日期=日期;
}
}
公共静态列表getHolidayList(整年)
{
int第一周=1;
int第二周=2;
int ThirdWeek=3;
int四周=4;
上周整数=5;
List HolidayList=新列表();
//   http://www.usa.gov/citizens/holidays.shtml      
//   http://archive.opm.gov/operating_status_schedules/fedhol/2013.asp
//元旦1月1日
添加(新假日(“新年”),新日期时间(vYear,1,1));
//马丁·路德·金,小,一月第三个星期一
添加(新假日(“MLK”,GetnthDayOfTweek(新日期时间(vYear,1,1),DayOfWeek.Monday,ThirdWeek));
//华盛顿的生日在二月的第三个星期一
HolidayList.Add(新假日(“WashingtonsBDay”,GetnthDayOfThweek(新日期时间(vYear,2,1),DayOfWeek.Monday,ThirdWeek));
//五月最后一个星期一是阵亡将士纪念日
添加(新假日(“纪念日”,GetnthDayOfThweek(新日期时间(vYear,5,1),DayOfWeek.Monday,LastWeek));
//独立日7月4日
添加(新假日(“独立日”,新日期时间(vYear,7,4));
//劳动节9月的第一个星期一
添加(新假日(“劳动日”,GetnthDayOfThweek(新日期时间(vYear,9,1),DayOfWeek.Monday,FirstWeek));
//哥伦布日十月第二个星期一
添加(新假日(“哥伦布”,GetnthDayOfThweek(新日期时间(vYear,10,1),DayOfWeek.Monday,SecondWeek));
//退伍军人节11月11日
添加(新假日(“退伍军人”,新日期时间(vYear,11,11));
//感恩节11月第四个星期四
添加(新假日(“感恩节”,GetnthDayOfThweek(新日期时间(vYear,11,1),DayOfWeek.周四,FourthWeek));
//圣诞节12月25日
添加(新假日(“圣诞节”,新日期时间(vYear,12,25));
//周六假期改为周五;周日改为周一
foreach(度假列表中的var假日)
{
if(假日.Date.DayOfWeek==星期六)
{
假日.Date=假日.Date.AddDays(-1);
}
如果(假日.Date.DayOfWeek==星期天,星期日)
{
假日.Date=假日.Date.AddDays(1);
}
}
//返回
返回度假清单;
}
private static System.DateTime getnthdayofthweek(DateTime dt,DayOfWeek-DayOfWeek,int WhichWeek)
{
//指定一个月中哪一周的哪一天,此函数将获取日期
//此函数使用提供日期的月份和年份
//获取给定日期的第一天
System.DateTime dtFirst=新日期时间(dt.年,dt.月,1);
//获得本月的第一天
System.DateTime dtRet=dtFirst.AddDays(6-(int)dtFirst.AddDays(-1*((int)dayofWeek+1)).dayofWeek);
//哪一周
dtRet=dtRet.AddDays((其中一周-1)*7);
//若一天已过月底,则向后调整一周
如果(dtRet>=dtFirst.AddMonths(1))
{
dtRet=dtRet.AddDays(-7);
}
//返回
返回dtRet;
}
静态void Main(字符串[]参数)
{
对于(int ii=2013;ii<2100;ii++)
{
var假期=getHolidayList(ii);
foreach(假日中的var假日)
{
Console.WriteLine(holiday.HolidayName+“:“+holiday.Date.ToSortDateString());
}
}
}
}

以下是我用C#编写的一些代码。每个假日都表示为假日的一个实例。要获得联邦政府遵守的日期,只需调用对象的FederalObservance方法。 但是,请注意,只有一个假日是实际假日;我刚刚创建了两个假期作为示例

        public static void Main (string[] args)
    {
        Holiday SatHoliday = new Holiday ("Satman", 2015, 11, 20);
        Holiday Thanksgiving = new Holiday ("Thanksgiving", 2015, 11, 3, DayOfWeek.Thursday);
        Holiday[] holidays = new Holiday[] { SatHoliday, Thanksgiving };

    }
}
class Holiday
{
    public DateTime hDate;
    public String name;
    public Holiday(String name, int year, int month, int day)
    {
        this.name = name;
        this.hDate = new DateTime (year, month, day);
    }
        public Holiday(String name, int year, int month, int weekNum/* Weeks are numbered starting with 0. So, the first week would be 0, the second week 1, etc.*/, DayOfWeek dayOfWeek)
    {
        this.name = name;
        DateTime firstOfMonth = new DateTime (year, month, 1);
        this.hDate = firstOfMonth.AddDays (7 * weekNum + (int)dayOfWeek - (int)firstOfMonth.DayOfWeek);
    }
    public DateTime FederalObservance()
    {

                    if (hDate.DayOfWeek == DayOfWeek.Saturday) {
                        return hDate.AddDays (-1);
                    } else if (hDate.DayOfWeek == DayOfWeek.Sunday) {
                        return hDate.AddDays (1);
        } else {
            return hDate;
        }
    }
}

Date支持美国的公共假日。许多其他国家都支持完整的源代码

Nuget

PM> install-package Nager.Date
示例:

享受一年中所有的公共假期

var publicHolidays = DateSystem.GetPublicHoliday("US", 2017);
检查日期是否为公共假日

if (DateSystem.IsPublicHoliday(date, "US"))
{
    //logic
}
下面是一个使用Lync的C#解决方案。我没有看到使用这种方法的任何其他答案。我把一个月的所有日子都放在DateTime类型的列表中,然后使用Lync精确地选择我想要的。是的,这需要更多的时间/m
if (DateSystem.IsPublicHoliday(date, "US"))
{
    //logic
}
/// <summary>
/// Find the Nth day of the month that matches the given dayOfWeek
/// </summary>
/// <param name="year">The year</param>
/// <param name="month">The month number 1 - 12</param>
/// <param name="dayOfWeek">The day of the week</param>
/// <param name="occurance">Take the Nth day found</param>
/// <returns></returns>
public static DateTime GetNthDayOfMonth(int year, int month, DayOfWeek dayOfWeek, int occurance)
{
    return GetDaysInMonth(year, month).Where(d => d.DayOfWeek == dayOfWeek).Skip(occurance - 1).FirstOrDefault();
}

/// <summary>
/// Find the Nth last day of the month that matches the given dayOfWeek
/// </summary>
/// <param name="year">The year</param>
/// <param name="month">The month number 1 - 12</param>
/// <param name="dayOfWeek">The day of the week</param>
/// <param name="occurance">Take the Nth day found</param>
/// <returns></returns>
public static DateTime GetNthLastDayOfMonth(int year, int month, DayOfWeek dayOfWeek, int occurance)
{
    return GetDaysInMonth(year, month).Where(d => d.DayOfWeek == dayOfWeek).Reverse().Skip(occurance - 1).FirstOrDefault();
}

/// <summary>
/// Put all dates of the month into a list that lync can work with
/// </summary>
/// <param name="year">The year</param>
/// <param name="month">The month number 1 - 12</param>
/// <returns></returns>
public static List<DateTime> GetDaysInMonth(int year, int month)
{
    List<DateTime> days = new List<DateTime>();
    var d = new DateTime(year, month, 1);
    while(d.Month == month)
    {
        days.Add(d);
        d = d.AddDays(1);
    }
    return days;
}
Sub displayHolidays()
    Dim myHolidayList As New Collection
    Set myHolidayList = GetHolidayList(2005)

    For x = 1 To (myHolidayList.Count)
        Debug.Print myHolidayList(x)
    Next
End Sub

Function GetHolidayList(ByVal vYear As Integer) As Collection
    Const FirstWeek As Integer = 1
    Const SecondWeek As Integer = 2
    Const ThirdWeek As Integer = 3
    Const FourthWeek As Integer = 4
    Const LastWeek As Integer = 5
    Const DayofWeek_Monday As Integer = 2
    Const DayofWeek_Thursday As Integer = 5
    Const DayofWeek_Saturday As Integer = 7
    Const DayofWeek_Sunday As Integer = 1

    Dim holidayList As New Collection
    Dim finalHolidayList As New Collection

    '   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 Each dt In holidayList
        If Weekday(dt) = DayofWeek_Saturday Then
            dt = DateAdd("d", -1, dt)
        End If
        If Weekday(dt) = DayofWeek_Sunday Then
            dt = DateAdd("d", 1, dt)
        End If
        finalHolidayList.Add dt
    Next dt

    'return
    Set GetHolidayList = finalHolidayList
End Function

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
    Dim dtFirst As Date
    Dim innerDate As Date
    Dim monthAdd As Date

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

    'get first DayOfWeek of the month
    innerDate = DateAdd("d", -DayofWeek, dtFirst)
    GetNthDayOfNthWeek = DateAdd("d", (7 - Weekday(innerDate)), dtFirst)

    'get which week
    GetNthDayOfNthWeek = DateAdd("d", ((WhichWeek - 1) * 7), GetNthDayOfNthWeek)

    'if day is past end of month then adjust backwards a week
    monthAdd = DateAdd("m", 1, dtFirst)
    If GetNthDayOfNthWeek >= monthAdd Then
        GetNthDayOfNthWeek = DateAdd("d", (-7), GetNthDayOfNthWeek)
    End If
End Function