/* Calendar.js - A simple JavaScript calendar generator 2022 by mercury0x0d, Creative Commons Attribution 4.0 International License Revision history: 2022-08-05 - Added CalendarDayPart(), and also corrected a typo 2022-07-22 - Initial version */ function CalendarCreate(year, month, day, element) { /* Generates a calendar inside the DIV specified by element for the month, day, and year specified Adapted from https://w3schoolweb.com/how-to-create-a-calendar-in-html-and-css with the following changes: - Dependency upon JQuery removed - JavaScript changed to dynamically add the calendar details to make it easier to dynamically spawn calendars - Now properly accounts for leap years - Physically shrunken so as to not take up so much dang space Input: year The year of the base date upon which the calendar will be generated month The month (0 to 11) of the base date upon which the calendar will be generated day The day of the base date upon which the calendar will be generated element A DIV element inside which the calendar will be built Output: n/a */ var newDIV, newUL, newLI; var startDay = new Date(year, month, 1).getDay(); var dayNames = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]; var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; var monthDaysCount = ["31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"]; var prevMonth = monthDaysCount[month - 1]; // adjust for yeap year if (CalendarYearIsLeap(year)) monthDaysCount[1]++; // adjust for the January/December crossover if (month == 11) { prevMonth = monthDaysCount[0]; } else if (month == 0) { prevMonth = monthDaysCount[11]; } var totalDays = monthDaysCount[month]; var counter = 0; var precounter = prevMonth - (startDay - 1); var rightbox = 6; var flag = true; // create a DIV for the month name at the top of the calendar and set the name to it newDIV = document.createElement("div"); newDIV.classList.add("CalendarHeaderMonth"); newDIV.innerText = monthNames[month].toUpperCase() + " " + year; element.appendChild(newDIV); // create a DIV for the header showing the days of the week and set the week names to it newDIV = document.createElement("div"); newDIV.classList.add("CalendarHeaderDay"); newUL = document.createElement("ul"); newUL.classList.add("CalendarUL"); for (var i = 0; i < dayNames.length; i++) { newLI = document.createElement("li"); newLI.innerHTML = dayNames[i]; newUL.appendChild(newLI); } newDIV.appendChild(newUL); element.appendChild(newDIV); // create a DIV for the header showing the days of the week and set the week names to it newDIV = document.createElement("div"); newDIV.classList.add("CalendarDateSquares"); newUL = document.createElement("ul"); newUL.classList.add("CalendarUL"); newDIV.appendChild(newUL); element.appendChild(newDIV); // populate that calendar! for (var i = 0; i < 42; i++) { if (i >= startDay) { counter++; if (counter > totalDays) { counter = 1; flag = false; } if (flag == true) { // we're on one of the days of the current month - create a new LI and set it accordingly newLI = document.createElement("li"); newLI.innerHTML = counter; // since we're in the current month, check to see if we're on the current day and change the background color if so if (counter == day) { newLI.style.color = "#FFFFFF"; newLI.style.backgroundColor = "#02548B"; } } else { // we're on one of the days of the next month - create a new LI and set it accordingly newLI = document.createElement("li"); newLI.style.backgroundColor = "#555"; newLI.innerHTML = counter; } } else { // we're on one of the days of the previous month - create a new LI and set it accordingly newLI = document.createElement("li"); newLI.style.backgroundColor = "#555"; newLI.innerHTML = precounter; precounter++; } if (i == rightbox) { newLI.classList.add("CalendarDateSquareRight"); rightbox = rightbox + 7; } if (i > 34) { newLI.classList.add("CalendarDateSquareBottom"); } // set a couple things that would conflict if we relied upon the CSS, then tack this onto the parent newLI.style.width = 39; newLI.style.lineHeight = "30px"; newDIV.firstElementChild.appendChild(newLI); } } function CalendarDayIsWeekday(year, month, day) { /* Determines if the day specified is a weekday Input: month The month of the date day The day of the date year The year of the date Output: Boolean true - The day is a weekday false - The day is not a weekday */ var day = new Date(year, month, day).getDay(); return day != 0 && day != 6; } function CalendarDayPart(dateObject) { /* Returns the part (morning, afternoon, evening) of the day specified Adapted from https://www.encodedna.com/javascript/say-good-morning-afternoon-evening-using-javascript.htm Input: dateObject The date object whose hour will be evaluated Output: String morning The hour of the date object provided was less than 12 afternoon The hour of the date object provided was between 12 and 17 (noon and 5:59 PM) evening The hour of the date object provided was between 18 and 23 (6:00 PM and 11:59 PM) */ var hour = dateObject.getHours(); var part; if (hour < 12) part = 'morning'; else if (hour >= 12 && hour <= 17) part = 'afternoon'; else if (hour >= 18 && hour <= 23) part = 'evening'; return part; } function CalendarDaysInMonth(month, year) { /* Returns the number of days in the month specified of the year specified Input: month The month of the date year The year of the date Output: Number of days in the month given */ return new Date(year, month + 1, 0).getDate(); } function CalendarMonthWeekdaysCount(month, year) { /* Returns the number of weekdays in the month specified of the year specified Input: month The month of the date year The year of the date Output: Number of weekdays in the month given */ var days = CalendarDaysInMonth(month, year); var weekdays = 0; for (var i = 0; i < days; i++) { if (CalendarDayIsWeekday(year, month, i+1)) weekdays++; } return weekdays; } function CalendarWeekdaysToDate(month, day, year) { /* Returns the number of weekdays between now and the date specified Input: month The month of the date day The day of the date year The year of the date Output: Number of weekdays between now and the date specified */ var weekdays = 0; for (var i = 0; i < day; i++) { if (CalendarDayIsWeekday(year, month, i+1)) weekdays++; } return weekdays; } function CalendarYearIsLeap(year) { /* Determines if the given date is a leap year From code found at https://stackoverflow.com/questions/16353211/check-if-year-is-leap-year-in-javascript Input: year The year to be tested Output: Boolean true - The year is a leap year false - The year is not a leap year */ return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); }