pub.solar/hakken-dates.js

110 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2022-10-02 19:38:38 +00:00
(() => {
const skippedMonths = [
{ m: 8, y: 2022 },
{ m: 12, y: 2022 },
];
2022-08-15 10:27:14 +00:00
2022-10-02 19:38:38 +00:00
const i18n = {
en: {
comingDates: 'The following dates are scheduled:',
friday: 'friday',
sunday: 'sunday',
until: 'until',
months: [
'Jan.',
'Feb.',
'Mar.',
'Apr.',
'May',
'Jun.',
'Jul.',
'Aug.',
'Sep.',
'Oct.',
'Nov.',
'Dec.',
],
},
de: {
comingDates: 'Folgende Termine stehen an:',
friday: 'Freitag',
sunday: 'Sonntag',
until: 'bis',
months: [
'Jan.',
'Feb.',
'Mär.',
'Apr.',
'Mai',
'Jun.',
'Jul.',
'Aug.',
'Sep.',
'Okt.',
'Nov.',
'Dez.',
],
},
};
2022-08-15 10:27:14 +00:00
2022-10-02 19:38:38 +00:00
const datesLists = {
en: document.getElementById('dates-list-en'),
de: document.getElementById('dates-list-de'),
};
2022-08-15 10:27:14 +00:00
2022-10-02 19:38:38 +00:00
const day = 24 * 60 * 60 * 1000;
const week = 7 * day;
const endOfWeekend = 2 * day;
const start = new Date();
const hakkens = [];
// We'll be looking about half a year into the future
for (let i = 0; i < 185; i++) {
const dateToTry = new Date(start.valueOf() + (i * day));
if (dateToTry.getDay() !== 5) {
continue;
}
2022-08-15 10:27:14 +00:00
2022-10-02 19:38:38 +00:00
const oneWeekLater = new Date(dateToTry.valueOf() + week);
if (dateToTry.getMonth() === oneWeekLater.getMonth()) {
continue;
}
2022-08-15 10:27:14 +00:00
2022-10-02 19:38:38 +00:00
const m = dateToTry.getMonth() + 1;
const y = dateToTry.getFullYear();
if (skippedMonths.find((s) => s.m === m && s.y === y)) {
continue;
}
hakkens.push(dateToTry);
2022-08-15 10:27:14 +00:00
}
2022-10-02 19:38:38 +00:00
// Helper function to add leading zeros to dates
const d = (num) => ('0' + num).slice(-2);
2022-08-15 10:27:14 +00:00
2022-10-02 19:38:38 +00:00
const writeText = (lang) => {
const p = document.createElement('p');
p.innerHTML = i18n[lang].comingDates;
datesLists[lang].appendChild(p);
}
2022-08-15 10:27:14 +00:00
2022-10-02 19:38:38 +00:00
const writeDateList = (lang) => {
const ul = document.createElement('ul');
hakkens.forEach(hakken => {
hakkend = new Date(hakken.valueOf() + endOfWeekend);
const showFirstMonth = hakken.getMonth() !== hakkend.getMonth();
const li = document.createElement('li');
li.innerHTML = `
${i18n[lang].friday} ${d(hakken.getDate())}. ${showFirstMonth ? i18n[lang].months[hakken.getMonth()] : ''}
${i18n[lang].until}
${d(hakkend.getDate())}. ${i18n[lang].months[hakkend.getMonth()]}
`;
ul.appendChild(li);
});
datesLists[lang].appendChild(ul);
}
2022-08-15 10:27:14 +00:00
2022-10-02 19:38:38 +00:00
writeText('de');
writeText('en');
writeDateList('de');
writeDateList('en');
})();