как добавить код в скрипт?
есть такой скрипт...
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <style> body { margin: 0; padding: 0; } #holiday { display: none; background: hsl(0, 0%, 90%); border: 1px solid hsl(0, 0%, 50%); border-radius: 12px/9px; } #holiday:nth-last-child(1) { margin: 0px 10px 5px 10px; background-color: #999; } .public_holiday { color: hsl(340, 100%, 50%); } .holiday { color: hsl(210, 100%, 50%); } .birthday { color: hsl(120, 100%, 25%); } #holiday, .public_holiday, .holiday, .birthday { display: block; font: bold 24px/20px serif; text-align: center; padding: 0; } .public_holiday, .holiday, .birthday { border: 1px solid hsl(0, 0%, 50%); border-radius: 12px/9px; margin: 10px; padding: 10px; } </style> </head> <body> <div id="header"> <div id="logo"></div> <div id="block_time-data"></div> </div> <div id="holiday"></div> <script> var holidays = [ { 1: { start: 0, duration: 24, compliments: "С новым годом !", }, 7: { start: 0, duration: 24, compliments: "С рождеством !", }, }, { 23: { start: 0, duration: 24, compliments: "С 23 февраля !", }, }, { 8: { start: 0, duration: 24, compliments: "С 8 марта !" }, }, { 12: { start: 0, duration: 24, compliments: "С днём космонавтики !", }, }, { 1: { start: 0, duration: 24, compliments: "С 1 мая !" }, 2: [ { start: 6, duration: 24 * 7 - 6, compliments: '<span class="birthday">С ДНЮХОЙ !</span>', }, { start: 0, duration: 24, compliments: '<span class="holiday">С 2 мая !</span>', }, ], 9: { start: 0, duration: 24, compliments: "С 9 мая !" }, }, { 1: { start: 0, duration: 24, compliments: "С 1 июня, Всемирным днём родителей !", }, }, { 3: { start: 0, duration: 24, compliments: "3 июля, день независимости Республики Беларусь !", }, }, { 12: { start: 0, duration: 24, compliments: "12 августа международный день молодёжи !", }, }, { 1: { start: 0, duration: 24, compliments: "1 сентября, день знаний !", }, }, { 1: { start: 0, duration: 24, compliments: "1 октября, международный день пожилых людей !", }, }, { 7: { start: 0, duration: 24, compliments: "7 ноября, день октябрьской революции !", }, }, { 25: { start: 0, duration: 24, compliments: "С католическим рождеством !", }, }, ]; function catholicDate(year) { var a = year % 19; var b = year % 4; var c = year % 7; var k = Math.floor(year / 100); var p = Math.floor((13 + 8 * k) / 25); var q = Math.floor(k / 4); var m = (15 - p + k - q) % 30; var n = (4 + k - q) % 7; var d = (19 * a + m) % 30; var e = (2 * b + 4 * c + 6 * d + n) % 7; if (d === 29 && e === 6) return new Date(year, 3, 19); if (d === 28 && e === 6 && (11 * m + 11) % 30 < 19) return new Date(year, 3, 18); if (d + e > 9) return new Date(year, 3, d + e - 9); else return new Date(year, 2, 22 + d + e); } function orthodoxDate(year) { var a = year % 19; var b = year % 4; var c = year % 7; var d = (19 * a + 15) % 30; var e = (2 * b + 4 * c + 6 * d + 6) % 7; var f = d + e; return f <= 26 ? new Date(year, 3, 4 + f) : new Date(year, 4, f - 26); } const today = new Date("2 May 2021 07:00:00:001"); // const today = new Date( "9 May 2021 00:00:00:001" ) // кат.пасха "4 April 2021 00:00:00:000" // прав.пасха "2 May 2021 00:00:00:000" // радуница "11 May 2021 00:00:00:000" // троица "20 June 2021 00:00:00:000" function getActualCompliments(now) { const result = []; holidays.forEach((vm, month) => { for (const day in vm) { let hlds = vm[day]; if (!Array.isArray(hlds)) hlds = [hlds]; for (const hld of hlds) { const currentDate = new Date( now.getFullYear(), month, +day, +hld.start ); const rg = now.getTime() - currentDate.getTime(); if (0 <= rg && rg < hld.duration * 3600000) { result.push(hld.compliments); } } } }); const caholic = catholicDate(now.getFullYear()); const ortodox = orthodoxDate(now.getFullYear()); if ( caholic.getMonth() == now.getMonth() && caholic.getDate() == now.getDate() ) result.push("С католической пасхой!"); if ( ortodox.getMonth() == now.getMonth() && ortodox.getDate() == now.getDate() ) result.push( '<span class="public_holiday">С православной пасхой!</span>' ); const radunitsa = new Date(ortodox); radunitsa.setDate(radunitsa.getDate() + 9); if ( radunitsa.getMonth() == now.getMonth() && radunitsa.getDate() == now.getDate() ) result.push("С радуницей !"); const trinity = new Date(ortodox); trinity.setDate(trinity.getDate() + 49); if ( trinity.getMonth() == now.getMonth() && trinity.getDate() == now.getDate() ) result.push("С троицей!"); return result; } const actualCompliments = getActualCompliments(today); console.log(actualCompliments); if (actualCompliments.length > 0) { const hollyday = document.getElementById("holiday"); hollyday.style.display = "block"; for (const cl of ["public_holiday", "holiday", "birthday"]) { for (const c of actualCompliments) { if (c.indexOf('class="' + cl + '"')>=0) { const div = document.createElement("div"); hollyday.appendChild(div); div.innerHTML = c; } } } /* for (var c of actualCompliments) { const div = document.createElement("div"); hollyday.appendChild(div); div.innerHTML = c; }*/ } </script> <div id="footer"></div> </body> </html> народ, в году есть несколько событий (праздников) которые отмечают не конкретного числа, а в определённый день недели конкретного месяца, например третий вторник октября, или второй понедельник апреля и тд или отсчёт может идти с конца месяца, например последняя суббота сентября, или предпоследняя пятница февраля, (в месяце ведь может быть и 4 и 5 дней недели) Вопрос такой, как высчитывать дату в этих обоих случаях? каким именно способом определить число(дату)? посоветовали что-то типа такого добавить... const holiday = { month: 6, //месяц праздника day: 7, //номер дня недели number: -1 // номер недели — отрицательный индекс означает что счёт ведётся с конца }; //День Военно-морского флота function getDate(date, year) { const d = new Date(); d.setFullYear(year); d.setMonth(holiday.month, 1); const firstDay = 1 + holiday.day + (d.getDay() < holiday.day ? -d.getDay() : 7 - d.getDay()); const days = [firstDay]; let day = firstDay; while ( new Date(year, holiday.month, (day += 7)).getMonth() == holiday.month ) { days.push(day); } const index = (holiday.number < 0 ? days.length : 0) + holiday.number; //return days[index]; return new Date(year, holiday.month, days[index]).toLocaleString(); } console.log(getDate(holiday, 2022)); народ, помогите добавить несколько подобных событий в этот скрипт... |
никто не знает ?
|
Часовой пояс GMT +3, время: 05:30. |