Open In App

Moment.js German Holiday (Feiertag) Plugin

Last Updated : 17 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn about moment-feiertage, a moment.js plugin. 

What is moment-feiertage?

Moment-feiertage is a Moment.js plugin that is used to determine whether a date, passed in the argument, is a german holiday. The list of holidays, that the package uses, is referred from Wikipedia (de) page.

Installation of the plugin:

You can install the package using the below command – 

npm install moment-feiertage

After that, create a folder and add a file, for example, main.js.

How to initialise/use

// Typescript
import * as moment from 'moment-feiertage';

// node
const moment = require('moment-feiertage');

Project structure: Now, your project structure should be like the below – 

 

Supported methods of moment-feiertage plugin

1. getAllStateCodes()

Return Value: It returns an array of all the state codes supported by the plugin. 

Example:

main.js




const moment = require('moment-feiertage');
console.log(moment.getAllStateCodes());


Step to run application:

node main.js

Output:

['BW','BY','BE','BB','HB','HH','HE','MV',
'NI','NW','RP','SL','SN','ST','SH','TH']

2. getHolidaysByYear (year)

Return Value: It returns an object containing all the holidays present in the year, which is passed as an argument to the method. 

Syntax:

getHolidaysByYear(Number)

Parameter: It accepts a single parameter of data-type number, which denotes the year for which you are trying to find holidays.

Example:

main.js




const moment = require('moment-feiertage');
  
// Represents 1981/07/17
console.log(moment.getHolidaysByYear(2020))


Steps to run application:

node main.js

Output:

 

{
  'Neujahrstag': {
    date: moment('2020-01-01'),
    state: [] // nationwide holiday
  },
  'Heilige Drei Könige': {
    date: moment('2020-01-06')
    
    // Only these states celebrate
    state: ['BW', 'BY', 'ST']
  },
  [ ... ]
}

3. isHoliday (states)

Return Value: It returns an object giving details about whether the date mentioned is a holiday in the states passed as arguments to the method.

Syntax:

moment(date: String).isHoliday(states: Array);

Parameter: moment takes a single parameter, date, as a string. isHoliday takes an array as an argument, denoting list of states.

Response:

{
  allStates: boolean, // default false
  holidayName: string, // default: ''
  holidayStates: Array<string>, // default: []
  testedStates: Array<string> // default: ...allStates
}

Example:

main.js




const moment = require('moment-feiertage');
  
console.log(moment('2018-11-01').isHoliday([]));


Steps to run application:

node main.js

Output:

 

4. isHoliday (state)

Return Value: It returns the name of the holiday if the mentioned date is actually a holiday, else it returns false. 

Syntax:

moment(date: String).isHoliday(state?: String);

Parameter: moment takes a single parameter, date, as a string. isHoliday takes an optional parameter string as an argument, denoting the state for which you want to check the holiday.

Example:

main.js




const moment = require('moment-feiertage');
console.log(moment('2017-08-15').isHoliday())


Steps to run application:

node main.js

Output:

 

State Codes: List of various state codes and their meanings –

BW = Baden-Württemberg
BY = Bayern
BE = Berlin
BB = Brandenburg
HB = Bremen
HH = Hamburg
HE = Hessen
MV = Mecklenburg-Vorpommern
NI = Niedersachsen
NW = Nordrhein-Westfalen
RP = Rhineland-Pfalz
SL = Saarland
SN = Sachsen
ST = Sachsen-Anhalt
SH = Schleswig-Holstein
TH = Thüringen

Reference: https://momentjscom.readthedocs.io/en/latest/moment/10-plugins/22-german-holiday/



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads