Code Monkey home page Code Monkey logo

moment-feiertage's Introduction

moment-feiertage

moment-feiertage is a Moment.js plugin to determine if a date is a german holiday. Holidays are taken from Wikipedia (de). Feel free to contribute!

How to use?

  1. Add moment-feiertage to your package.json by running npm install moment-feiertage --save. Moment.js is a peer dependency, so don't forget to install it, if you haven't already.
  2. Import moment from moment-feiertage like you would from the original Moment.js package. moment-feiertage exports the original moment object with extended functionality.
// Typescript
import * as moment from 'moment-feiertage';

// node
const moment = require('moment-feiertage');
  1. Check the examples below for functionality, supported arguments and return values.

getAllStateCodes()

since 2.0.0

const codes = moment.getAllStateCodes();
/* returns ['BW','BY','BE','BB','HB','HH','HE','MV','NI','NW','RP','SL','SN','ST','SH','TH']*/

getHolidaysByYear(year: number)

since 2.0.0

Returns an object containing all holidays of a year. Every holiday has a date and a state property. date is holding a moment object representing the holidays date. It's a nationwide holiday, if the state value is an empty Array.

const codes = moment.getHolidaysByYear(2020);
/* returns {
  'Neujahrstag': {
    date: moment('2020-01-01'),
    state: [] // nationwide holiday
  },
  'Heilige Drei Könige': {
    date: moment('2020-01-06')
    state: ['BW', 'BY', 'ST'] // only these states celebrate
  },
  [ ... ]
} */

isHoliday(states: Array)

since 1.1.0

From version 1.1.0 on isHoliday() supports Arrays. Pass an empty Array to test against all states, or pass an Array of state codes (e.g. ['BY', 'SH']). The return value is an Object:

{
  allStates: boolean, // default false
  holidayName: string, // default: ''
  holidayStates: Array<string>, // default: []
  testedStates: Array<string> // default: ...allStates
}
  • allStates is true, if the checked date is a nationwide holiday, even if not all states are checked because of the states param.
  • holidayName contains the name of the holiday
  • holidayStates contains the states, where this holiday is celebrated. If states param is provided, holidayStates contains only a subset of states
  • testedStates is the same as the states param. If states param is [], isHoliday will check against all states by default
const christmasInAllStates = moment('2018-11-01').isHoliday([]);
/* returns {
  allStates: true,
  holidayName: '1. Weihnachtsfeiertag',
  holidayStates: ...allStates,
  testedStates: ...allStates
}*/
const christmasInSomeStates = moment('2018-11-01').isHoliday(['BW', 'SH']);
/* returns {
  allStates: true,
  holidayName: '1. Weihnachtsfeiertag',
  holidayStates: [ 'BW', 'SH' ],
  testedStates: [ 'BW', 'SH' ]
}*/
const someDateInAllStates = moment('2018-11-01').isHoliday([]);
/* returns {
  allStates: false,
  holidayName: 'Allerheiligen',
  holidayStates: [ 'BW', 'BY', 'NW', 'RP', 'SL' ],
  testedStates: ...allStates
}*/
const noHolidayDateInAllStates = moment('2018-12-12').isHoliday([]);
/* returns {
  allStates: false,
  holidayName: '',
  holidayStates: [],
  testedStates: ...allStates
}*/

isHoliday(state?: string)

since 1.0.0

Since version 1.0.0 isHoliday() checks if there's a holiday at a moment object. A state code can be provided optionally.

const nowIsHoliday = moment().isHoliday();
// returns name of holiday (string) if date is a holiday
// retruns false (boolean) if date is not a holiday

const someDateIsHoliday = moment('2019-12-25').isHoliday();
// returns '1. Weihnachtsfeiertag' - is a holiday in all states

const isHolidayInAllStates = moment('2017-08-15').isHoliday();
// returns false - is not a holiday in all states

const isHolidayInBavaria = moment('2017-08-15').isHoliday('BY');
// returns false - is not a holiday in BY

const isHolidayInSaarland = moment('2017-08-15').isHoliday('SL');
// returns 'Mariä Himmelfahrt' - is a holiday in SL

State codes

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 = Rheinland-Pfalz
SL = Saarland
SN = Sachsen
ST = Sachsen-Anhalt
SH = Schleswig-Holstein
TH = Thüringen

Mappings

Contribute

  1. fork
  2. npm install and add your desired version of Moment.js: npm install moment --no-save
  3. code
  4. npm run build: linting, formating, building, testing
  5. PR

moment-feiertage's People

Contributors

acertaincoder avatar andreaswillems avatar danischenk avatar dependabot[bot] avatar froggs avatar nilsmehlhorn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

moment-feiertage's Issues

Add typescript definition

Hey,
it would be great if the project contains a typescript index.d.ts file. So in typescript projects as in Angular the compiler doesn't claim that the function isHoliday doesn't exist.

Maria Himmelfahrt in Bayern

Hi @DaniSchenk,

erstmal vielen Dank, dass du dir die Mühe gemacht hast ein Plugin für die deutschen Feiertage zu schreiben und es dann der Community zur Verfügung stellst :)

Da du ja selbst aus München kommst, bist du bestimmt selbst schon über den Fall gestolpert, dass du Maria Himmelfahrt gerne als Feiertag betrachten möchtest da er fast in ganz Bayern ein Feiertag ist.

Aus deiner Dokumentation
const isHolidayInBavaria = moment('2017-08-15').isHoliday('BY'); // returns false - is not a holiday in BY

Gibt es eine Methode mit der sich dieses Verhalten steuern lässt oder wie handhabst du solche Fälle.

Danke für deine Zeit.

Liebe Grüße

Tobi

Holidays are only recognized at 00:00

A holiday should be recognized on the whole day.
The example snippet const nowIsHoliday = moment().isHoliday() in the README suggests, that this is also meant to be in the feature scope.

Example Code:

const moment = require('moment-feiertage')

console.log(moment('2020-12-25 17:00').isHoliday()) // false
console.log(moment('2020-12-25 00:00').isHoliday()) // 1. Weihnachtsfeiertag

A possible fix would probably be, to change

if (_moment.isSame(holidays[h].date)) {

to

if (_moment.isSame(holidays[h].date, 'day')) { 

publish new version in npm

hey thank you for that plugin.

You merged the commit for console.log but don't published a new version to npm. So I still get huge console.logs wen using your plugin.

Can you please update the version in your package.json and make npm publish again?

Fix peerDependency issue

Hi, I use your plugin and it's very useful for me.
But now I have to update moment to version 2.29.
Will you update your peerDependency in the next time?
Best regards,
Peter

Originally posted by @pit999 in #28 (comment)

React native support

On react-native isHoliday() is always undefined. Any idea how to add RN support?

moment-feiertage does not use Google Places compliant values for states

If you use the Google Places API, for the query of the federal states to determine the holidays, then you have to remap the value for Lower Saxony (NDS) to the plugin compatible value (NI).

So it would be a good idea to use the values that Google uses for administrative_area_level_1 as short_name also as check for the federal states

Add Frauentag for MV

MV decided to celebrate the Internationaler Frauentag (8th of march) from 2023 on

Problem mit der for-in-Schleife

Hallo, mir ist per Zufall dieses Fehler aufgefallen:

TypeError: Cannot read property 'length' of undefined
at _isHoliday106 (%PROJECT%\node_modules\moment-feiertage\build\index.js:53:39)
at _isHoliday (%PROJECT%\node_modules\moment-feiertage\build\index.js:85:23)

moment: ^2.29.1
moment-duration-format: ^2.3.2
moment-feiertage: ^2.0.3
var holiday = moment().isHoliday();

Ich bin den Part mit dem Debugger durchgegangen und dabei auf eine for-in-Schleife gestoßen. Problem ist, dass diese ebenfalls durch die Prototypen itteriert und dadurch Attribute verarbeitet, die keine Feiertage beinhalten:
Code_2020-12-14_17-33-32

Ich habe hier zwei Vorschläge mit denen man das Problem lösen könnte.

  1. via Object.keys und for-of:
for (var h of Object.keys(holidays)) {
    if (_moment.isSame(holidays[h].date, 'day')) {
        if (holidays[h].state.length === 0) {
            return h;
        }
        else {
            if (_state && holidays[h].state.indexOf(_state) > -1) {
                return h;
            }
            else {
                return false;
            }
        }
    }
}
  1. via Object.entries und for-of
for(var [h, data] of Object.entries(holidays)) {
    if (_moment.isSame(data.date, 'day')) {
        if (data.state.length === 0) {
            return h;
        }
        else {
            if (_state && data.state.indexOf(_state) > -1) {
                return h;
            }
            else {
                return false;
            }
        }
    }
}

Mit diesen Änderungen funktionierte das Modul bei mir wieder.
- ggf. erstelle ich auch gerne einen Pull Request

Viele Grüße
ACertainCoder

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.