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 奉獻下發布。透過提交拉取請求,您同意遵守版權利益的放棄。