Python Holidays ModuleIn this tutorial, we will learn one of the interesting Python libraries named holidays, which determines holidays of a given year of a particular day. It returns the specific result of the given date. We only need to pass the country name. However, it can only detect the fixed holidays such as Christmas, New Year, etc. InstallationType the following command to the terminal and press enter. Import the holidays module to check whether it is installed or not. The HolidayBase() MethodThe holidays module provides the HolidayBase() method, which is used to detect the holidays of a specific date. Let’s see the following syntax. Syntax – Parameters – It takes the following parameter.
Let’s understand the following example – Example – Output: (datetime.date(2020, 1, 1), "New Year's Day") (datetime.date(2020, 1, 20), 'Martin Luther King Jr. Day') (datetime.date(2020, 2, 17), "Washington's Birthday") (datetime.date(2020, 5, 25), 'Memorial Day') (datetime.date(2020, 7, 4), 'Independence Day') (datetime.date(2020, 7, 3), 'Independence Day (Observed)') (datetime.date(2020, 9, 7), 'Labor Day') (datetime.date(2020, 10, 12), 'Columbus Day') (datetime.date(2020, 11, 11), 'Veterans Day') (datetime.date(2020, 11, 26), 'Thanksgiving') (datetime.date(2020, 12, 25), 'Christmas Day') Example – 2 Output: (datetime.date(2021, 1, 14), 'Makar Sankranti / Pongal') (datetime.date(2021, 1, 26), 'Republic Day') (datetime.date(2021, 8, 15), 'Independence Day') (datetime.date(2021, 10, 2), 'Gandhi Jayanti') (datetime.date(2021, 5, 1), 'Labour Day') (datetime.date(2021, 12, 25), 'Christmas') Note – As we can see in the output, the holidays module only returns the fixed-date holidays.Example – 3: Output: (datetime.date(2021, 1, 1), "New Year's Day") (datetime.date(2021, 1, 2), 'New Year Holiday [Scotland]') (datetime.date(2021, 1, 4), 'New Year Holiday [Scotland] (Observed)') (datetime.date(2021, 3, 17), "St. Patrick's Day [Northern Ireland]") (datetime.date(2021, 7, 12), 'Battle of the Boyne [Northern Ireland]') (datetime.date(2021, 8, 2), 'Summer Bank Holiday [Scotland]') (datetime.date(2021, 11, 30), "St. Andrew's Day [Scotland]") (datetime.date(2021, 12, 25), 'Christmas Day') (datetime.date(2021, 12, 27), 'Christmas Day (Observed)') (datetime.date(2021, 4, 2), 'Good Friday') (datetime.date(2021, 4, 5), 'Easter Monday [England, Wales, Northern Ireland]') (datetime.date(2021, 5, 3), 'May Day') (datetime.date(2021, 5, 31), 'Spring Bank Holiday') (datetime.date(2021, 8, 30), 'Late Summer Bank Holiday [England, Wales, Northern Ireland]') (datetime.date(2021, 12, 26), 'Boxing Day') (datetime.date(2021, 12, 28), 'Boxing Day (Observed)') We can get the holiday’s list for the desired country. How to check a given date is a holiday?Suppose we want to check a specific date is a holiday or not. We can do this as follows. Example – Output: Yes: Christmas Day Custom Holidays Adding for IndiaMany countries are missed in the following table but luckily we can add own Custom holidays for a specific country. In the below example, we will add the custom holidays for India. Example – Output: False True Holi: Festival of Colors, Holiday Explanation: In the above code, first, we have imported the date and holidays modules. We have then created the object of HolidayBase() and checked whether a given date consists of a holiday. It retuned false, so we add the custom holiday without description. In the other statement, we have added the holiday with a description and printed the result. Available Countries in Holidays LibraryBelow are the countries that are supported by the holidays library.
|
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263546.html