⚠️ bc-holiday.js is still in development! Please let me know in the Issues
tab if you encounter any issues or would like holidays added to the list.
Include the JS library in your HTML documents like this:
<script src="script/bc-holiday.js"></script>
Or use the URL to the file in the GitHub repo:
<script src="https://raw.githubusercontent.com/The-Best-Codes/bc-holiday.js/main/bc-holiday.js"></script>
Example: Returns the holidays for the current day
const holidaysToday = bcHoliday.getHolidaysOn(new Date());
console.log(holidaysToday);
/*Write the days holidays to the console as an array*/
[]
Example: Get the holidays in November
Note that JavaScript is 0 based, so November, the 11th month, is 10 in JS
const holidaysThisMonth = bcHoliday.getHolidaysInRange(new Date(2023, 10, 1), new Date(2023, 10, 30));
console.log(holidaysThisMonth);
/*Write the days holidays to the console as an array*/
{"11-01":["All Saints' Day"],"11-11":["Veterans Day"],"11-23":["Thanksgiving"]}
Example: Show the holidays for November on an HTML page
Note that JavaScript is 0 based, so November, the 11th month, is 10 in JS
Click to view code
<!DOCTYPE html>
<html>
<head>
<title>Holiday List</title>
<style>
body {
font-family: Arial, sans-serif;
}
h1 {
text-align: center;
}
ul {
list-style-type: none;
padding: 0;
}
li {
margin-bottom: 10px;
}
.holiday-date {
font-weight: bold;
}
</style>
</head>
<body>
<h1>Holiday List</h1>
<ul id="holiday-list"></ul>
<script>
const holidaysData = bcHoliday.getHolidaysInRange(new Date(2023, 10, 1), new Date(2023, 10, 30));
const holidayListElement = document.getElementById('holiday-list');
for (const date in holidayData) {
const holidayName = holidayData[date][0]; // Assuming only one holiday per date
const listItem = document.createElement('li');
const dateElement = document.createElement('span');
dateElement.classList.add('holiday-date');
dateElement.textContent = formatDate(date);
listItem.appendChild(dateElement);
listItem.appendChild(document.createTextNode(': ' + holidayName));
holidayListElement.appendChild(listItem);
}
function formatDate(dateString) {
const [month, day] = dateString.split('-');
return `${getMonthName(month)} ${parseInt(day)}${getDaySuffix(parseInt(day))}`;
}
function getMonthName(month) {
const monthNames = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
];
return monthNames[parseInt(month) - 1];
}
function getDaySuffix(day) {
if (day >= 11 && day <= 13) {
return 'th';
}
switch (day % 10) {
case 1:
return 'st';
case 2:
return 'nd';
case 3:
return 'rd';
default:
return 'th';
}
}
</script>
<script src="script/path/to/bc-holiday.js"></script>
</body>
</html>
Example: Return the current day in MM-DD
format
bcHoliday.formatDate(new Date());
'01-01'
Example: Return the day on which Easter will occur for the supplied year
bcHoliday.calculateEaster(2030);
//Returns the date of Easter for the supplied year
Sun Apr 21 2030 00:00:00 GMT-0500 (Central Daylight Time)