@18f/us-federal-holidays 中文文档教程

发布于 3年前 浏览 30 项目主页 更新于 3年前

US Federal Holidays

建立并返回给定年份所有美国联邦假日的列表,以及 提供了一种辅助方法来确定给定日期是否为美国联邦假日。 如果假期落在 周末。

美国联邦假日由 OPM 定义

Installation

npm install @18f/us-federal-holidays

需要 Node.js 10 或更高版本。

Usage

要获得给定年份所有美国联邦假日的列表,请使用 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-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' } ]

要获取日期范围内所有美国联邦假日的列表,请使用 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.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' } ]

要确定日期是否为联邦假日,请使用 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;
}

Public domain

该项目属于全球公共领域< /a>。 如中所述 CONTRIBUTING

该项目在美国属于公共领域,版权所有 和世界范围内的工作相关权利通过放弃 CC0 1.0 通用公共领域贡献

所有对该项目的贡献都将在 CC0 奉献精神下发布。 通过提交拉取请求,您同意遵守此放弃 版权权益。

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 10 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.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文