pub.solar/scripts/hakken-dates.js

110 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-10-02 19:38:38 +00:00
const skippedMonths = [
{ m: 8, y: 2022 },
{ m: 12, y: 2022 },
{ m: 4, y: 2023 },
{ m: 12, y: 2023 },
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.',
],
},
};
const datesLists = {
en: document.getElementById('dates-list-en'),
de: document.getElementById('dates-list-de'),
};
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;
}
const oneWeekLater = new Date(dateToTry.valueOf() + week);
if (dateToTry.getMonth() === oneWeekLater.getMonth()) {
continue;
}
const m = dateToTry.getMonth() + 1;
const y = dateToTry.getFullYear();
if (skippedMonths.find((s) => s.m === m && s.y === y)) {
continue;
}
hakkens.push(dateToTry);
}
// Helper function to add leading zeros to dates
const d = (num) => ('0' + num).slice(-2);
const writeText = (lang) => {
const p = document.createElement('p');
p.innerHTML = i18n[lang].comingDates;
datesLists[lang].appendChild(p);
}
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);
}
writeText('de');
writeText('en');
writeDateList('de');
writeDateList('en');