@18f/us-federal-holidays

中文文档
4.0.0发布于 2023/04/22CC0-1.0TypeScriptCommonJS构建来源已验证近 30 天下载 53.6万

All about US federal holidays

原始使用文档
文档目录3 节
  1. Installation
  2. Usage
  3. Public domain

US Federal Holidays

Builds and returns a list of all US federal holidays for a given year, and provides a helper method to determine if a given date is a US federal holiday. Handles shifting holidays to the nearest weekday if the holiday falls on a weekend.

US federal holidays are as defined by OPM.

Installation

npm install @18f/us-federal-holidays

Requires Node.js 12 or higher.

Usage

To get a list of all US federal holidays in a given year, use the allForYear method. If no year is passed in, uses the current year.

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-01T00:00:00.000Z,
    dateString: '2016-1-1' },
  { name: 'Birthday of Martin Luther King, Jr.',
    date: 2016-01-18T00:00:00.000Z,
    dateString: '2016-1-18' },
  { name: 'Washington\'s Birthday',
    alsoObservedAs: 'Presidents\' Day',
    date: 2016-02-15T00:00:00.000Z,
    dateString: '2016-2-15' },
  { name: 'Memorial Day',
    date: 2016-05-30T00:00:00.000Z,
    dateString: '2016-5-30' },
  {
    name: 'Juneteenth National Independence Day',
    date: 2016-06-20T00:00:00.000Z,
    dateString: '2016-6-20' },
  { name: 'Independence Day',
    date: 2016-07-04T00:00:00.000Z,
    dateString: '2016-7-4' },
  { name: 'Labor Day',
    date: 2016-09-05T00:00:00.000Z,
    dateString: '2016-9-5' },
  { name: 'Columbus Day',
    alsoObservedAs: 'Indigenous Peoples\' Day',
    date: 2016-10-10T00:00:00.000Z,
    dateString: '2016-10-10' },
  { name: 'Veterans Day',
    date: 2016-11-11T00:00:00.000Z,
    dateString: '2016-11-11' },
  { name: 'Thanksgiving Day',
    date: 2016-11-24T00:00:00.000Z,
    dateString: '2016-11-24' },
  { name: 'Christmas Day',
    date: 2016-12-26T00:00:00.000Z,
    dateString: '2016-12-26' } ]

To get a list of all US federal holidays within a date range, use the inRange method. If no start date is provided in, uses the current date. If the end date is omitted, one year from the current date is used.

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.federalHolidaysInRange(start, end, options);

// Returns
[ { name: 'Washington\'s Birthday',
    date: 2016-02-15T00:00:00.000Z,
    dateString: '2016-2-15' },
  { name: 'Memorial Day',
    date: 2016-05-30T00:00:00.000Z,
    dateString: '2016-5-30' },
  {
    name: 'Juneteenth National Independence Day'
    date: 2016-06-20T00:00:00.000Z,
    dateString: '2016-6-20' },
  { name: 'Independence Day',
    date: 2016-07-04T00:00:00.000Z,
    dateString: '2016-7-4' },
  { name: 'Labor Day',
    date: 2016-09-05T00:00:00.000Z,
    dateString: '2016-9-5' },
  { name: 'Columbus Day',
    alsoObservedAs: 'Indigenous Peoples\' Day',
    date: 2016-10-10T00:00:00.000Z,
    dateString: '2016-10-10' },
  { name: 'Veterans Day',
    date: 2016-11-11T00:00:00.000Z,
    dateString: '2016-11-11' },
  { name: 'Thanksgiving Day',
    date: 2016-11-24T00:00:00.000Z,
    dateString: '2016-11-24' },
  { name: 'Christmas Day',
    date: 2016-12-26T00:00:00.000Z,
    dateString: '2016-12-26' },
  { name: 'New Year\'s Day',
    date: 2017-01-02T00:00:00.000Z,
    dateString: '2017-1-2' },
  { name: 'Birthday of Martin Luther King, Jr.',
    date: 2017-01-16T00:00:00.000Z,
    dateString: '2017-1-16' },
  { name: 'Washington\'s Birthday',
    alsoObservedAs: 'Presidents\' Day',
    date: 2017-02-20T00:00:00.000Z,
    dateString: '2017-2-20' },
  { name: 'Memorial Day',
    date: 2017-05-29T00:00:00.000Z,
    dateString: '2017-5-29' },
  {
    name: 'Juneteenth National Independence Day'
    date: 2017-06-19T00:00:00.000Z,
    dateString: '2017-6-19' },
  { name: 'Independence Day',
    date: 2017-07-04T00:00:00.000Z,
    dateString: '2017-7-4' } ]

To determine if a date is a federal holiday, use the isAHoliday method. If no argument is provided, defaults to the current date:

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

All three methods take options as a second argument. This argument is a plain object which accepts the following properties:

{
  // 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
}

Additionally, isAHoliday takes an options.utc parameter:

{
  // 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;
}

Public domain

This project is in the worldwide public domain. As stated in CONTRIBUTING:

This project is in the public domain within the United States, and copyright and related rights in the work worldwide are waived through the CC0 1.0 Universal public domain dedication.

All contributions to this project will be released under the CC0 dedication. By submitting a pull request, you are agreeing to comply with this waiver of copyright interest.

版本与发布记录

@18f/us-federal-holidays 共发布 19 个版本,首次发布于 2016/02/28;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.0.0latest2023/04/223 年前
3.0.22023/04/063 年前
3.0.12022/09/274 年前
3.0.02022/09/274 年前
2.1.12021/09/185 年前
2.1.02021/09/185 年前

关于 @18f/us-federal-holidays 的常见问题

@18f/us-federal-holidays 是什么?

All about US federal holidays

如何安装 @18f/us-federal-holidays?

在项目目录运行 npm install @18f/us-federal-holidays,或使用 pnpm add / yarn add / bun add。

@18f/us-federal-holidays 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@18f/us-federal-holidays 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@18f/us-federal-holidays 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@18f/us-federal-holidays 可以商用吗?

@18f/us-federal-holidays 使用 CC0-1.0 许可。作者放弃著作权,可不受限制地使用。以上为概括,不构成法律意见,请以许可原文为准。

@18f/us-federal-holidays 的下载量有多少?

根据 npm 官方下载统计,@18f/us-federal-holidays 在 2026/10/05 – 2026/11/03 这 30 天内被下载 536,029 次,日均约 17,868 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@18f/us-federal-holidays 还在维护吗?

最新版本 4.0.0 发布于 2023/04/22(3 年前);至今共发布 19 个版本,其中过去 12 个月发布了 0 个;首次发布于 2016/02/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。