us federal holidays
v4.0.0
构建并返回给定年份的所有美国联邦假日的列表,并提供辅助方法来确定给定日期是否为美国联邦假日。如果假期恰逢周末,则将假期转移到最近的工作日。
美国联邦假日由 OPM 定义。
npm install @18f/us-federal-holidays
需要 Node.js 12 或更高版本。
要获取给定年份中所有美国联邦假日的列表,请使用allForYear
方法。如果未传入年份,则使用当前年份。
const fedHolidays = require ( '@18f/us-federal-holidays' ) ;
const options = { shiftSaturdayHolidays : true , shiftSundayHolidays : true } ;
const holidays = fedHolidays . allForYear ( 2016 , options ) ;
// Returns
[ { name : 'New Year's Day' ,
date : 2016 - 01 - 01 T00 : 00 : 00 .000 Z ,
dateString : '2016-1-1' } ,
{ name : 'Birthday of Martin Luther King, Jr.' ,
date : 2016 - 01 - 18 T00 : 00 : 00 .000 Z ,
dateString : '2016-1-18' } ,
{ name : 'Washington's Birthday' ,
alsoObservedAs : 'Presidents' Day' ,
date : 2016 - 02 - 15 T00 : 00 : 00 .000 Z ,
dateString : '2016-2-15' } ,
{ name : 'Memorial Day' ,
date : 2016 - 05 - 30 T00 : 00 : 00 .000 Z ,
dateString : '2016-5-30' } ,
{
name : 'Juneteenth National Independence Day' ,
date : 2016 - 06 - 20 T00 : 00 : 00 .000 Z ,
dateString : '2016-6-20' } ,
{ name : 'Independence Day' ,
date : 2016 - 07 - 04 T00 : 00 : 00 .000 Z ,
dateString : '2016-7-4' } ,
{ name : 'Labor Day' ,
date : 2016 - 09 - 05 T00 : 00 : 00 .000 Z ,
dateString : '2016-9-5' } ,
{ name : 'Columbus Day' ,
alsoObservedAs : 'Indigenous Peoples' Day' ,
date : 2016 - 10 - 10 T00 : 00 : 00 .000 Z ,
dateString : '2016-10-10' } ,
{ name : 'Veterans Day' ,
date : 2016 - 11 - 11 T00 : 00 : 00 .000 Z ,
dateString : '2016-11-11' } ,
{ name : 'Thanksgiving Day' ,
date : 2016 - 11 - 24 T00 : 00 : 00 .000 Z ,
dateString : '2016-11-24' } ,
{ name : 'Christmas Day' ,
date : 2016 - 12 - 26 T00 : 00 : 00 .000 Z ,
dateString : '2016-12-26' } ]
要获取某个日期范围内所有美国联邦假日的列表,请使用inRange
方法。如果未提供start
日期,则使用当前日期。如果省略结束日期,则使用当前日期起的一年。
const fedHolidays = require ( '@18f/us-federal-holidays' ) ;
const start = new Date ( '2016-02-13' ) ;
const end = new Date ( '2017-07-23' ) ;
const options = { shiftSaturdayHolidays : true , shiftSundayHolidays : true } ;
const holidays = fedHolidays . inRange ( start , end , options ) ;
// Returns
[ { name : 'Washington's Birthday' ,
date : 2016 - 02 - 15 T00 : 00 : 00 .000 Z ,
dateString : '2016-2-15' } ,
{ name : 'Memorial Day' ,
date : 2016 - 05 - 30 T00 : 00 : 00 .000 Z ,
dateString : '2016-5-30' } ,
{
name : 'Juneteenth National Independence Day'
date : 2016 - 06 - 20 T00 : 00 : 00 .000 Z ,
dateString : '2016-6-20' } ,
{ name : 'Independence Day' ,
date : 2016 - 07 - 04 T00 : 00 : 00 .000 Z ,
dateString : '2016-7-4' } ,
{ name : 'Labor Day' ,
date : 2016 - 09 - 05 T00 : 00 : 00 .000 Z ,
dateString : '2016-9-5' } ,
{ name : 'Columbus Day' ,
alsoObservedAs : 'Indigenous Peoples' Day' ,
date : 2016 - 10 - 10 T00 : 00 : 00 .000 Z ,
dateString : '2016-10-10' } ,
{ name : 'Veterans Day' ,
date : 2016 - 11 - 11 T00 : 00 : 00 .000 Z ,
dateString : '2016-11-11' } ,
{ name : 'Thanksgiving Day' ,
date : 2016 - 11 - 24 T00 : 00 : 00 .000 Z ,
dateString : '2016-11-24' } ,
{ name : 'Christmas Day' ,
date : 2016 - 12 - 26 T00 : 00 : 00 .000 Z ,
dateString : '2016-12-26' } ,
{ name : 'New Year's Day' ,
date : 2017 - 01 - 02 T00 : 00 : 00 .000 Z ,
dateString : '2017-1-2' } ,
{ name : 'Birthday of Martin Luther King, Jr.' ,
date : 2017 - 01 - 16 T00 : 00 : 00 .000 Z ,
dateString : '2017-1-16' } ,
{ name : 'Washington's Birthday' ,
alsoObservedAs : 'Presidents' Day' ,
date : 2017 - 02 - 20 T00 : 00 : 00 .000 Z ,
dateString : '2017-2-20' } ,
{ name : 'Memorial Day' ,
date : 2017 - 05 - 29 T00 : 00 : 00 .000 Z ,
dateString : '2017-5-29' } ,
{
name : 'Juneteenth National Independence Day'
date : 2017 - 06 - 19 T00 : 00 : 00 .000 Z ,
dateString : '2017-6-19' } ,
{ name : 'Independence Day' ,
date : 2017 - 07 - 04 T00 : 00 : 00 .000 Z ,
dateString : '2017-7-4' } ]
要确定某个日期是否为联邦假日,请使用isAHoliday
方法。如果未提供参数,则默认为当前日期:
const fedHolidays = require ( "@18f/us-federal-holidays" ) ;
const options = {
shiftSaturdayHolidays : true ,
shiftSundayHolidays : true ,
utc : false
} ;
const isAHoliday = fedHolidays . isAHoliday ( myDate , options ) ;
// Returns true or false
所有三种方法都将options
作为第二个参数。该参数是一个普通对象,它接受以下属性:
{
// Whether or not holidays that fall on Saturdays should be
// shifted to Friday observance. If you don't follow the
// US federal standard for observing holidays on weekends,
// you can adjust by setting this value to false.
// Default value is true.
shiftSaturdayHolidays : boolean ,
// Whether or not holidays that fall on Sundays should be
// shifted to Monday observance. If you don't follow the
// US federal standard for observing holidays on weekends,
// you can adjust by setting this value to false.
// Default value is true.
shiftSundayHolidays : boolean
}
此外, isAHoliday
采用options.utc
参数:
{
// Whether to treat the first argument as a UTC date instead
// of the local time. Defaults to false. This is useful if
// you're generating dates from UTC timestamps or otherwise
// creating objects from UTC-based dates.
// Default value is false.
// This option only applies to the isAHoliday method.
utc: boolean ;
}
该项目属于全球公共领域。正如贡献中所述:
该项目在美国属于公共领域,并且通过 CC0 1.0 通用公共领域奉献放弃了全球作品的版权和相关权。
该项目的所有贡献都将在 CC0 奉献下发布。通过提交拉取请求,您同意遵守版权利益的放弃。