| function daysIntoSeason(date) { |
| return Math.floor((new Date().getTime() - date.getTime()) / (1000 * 60 * 60 * 24)) |
| } |
| |
| function daysSinceDate(date) { |
| // If the date is after today, make it last year |
| if (date > new Date()) { |
| date.setFullYear(date.getFullYear() - 1); |
| } |
| return Math.floor((new Date().getTime() - date.getTime()) / (1000 * 60 * 60 * 24)); |
| } |
| |
| function getSeason() { |
| let year = new Date().getFullYear(); |
| |
| const dates = { |
| pride: [new Date(`${year}-6-1`), new Date(`${year}-6-31`)], |
| halloween: [new Date(`${year}-10-25`), new Date(`${year}-11-1`)], |
| trans: [new Date(`${year}-11-13`), new Date(`${year}-11-19`)], |
| christmas: [new Date(`${year}-12-1`), new Date(`${year}-12-26`)], |
| aprilFools: [new Date(`${year}-04-01`), new Date(`${year}-04-01`)] |
| } |
| const filePaths = { |
| normal: "normal", |
| halloween: "seasonal/halloween", |
| christmas: "seasonal/christmas", |
| pride: "seasonal/pride", |
| trans: "seasonal/trans", |
| aprilFools: "seasonal/aprilFools" |
| } |
| |
| let current = new Date(); |
| let currentSeason = Object.keys(dates).find((str) => current >= dates[str][0] && current <= dates[str][1]) || "normal"; |
| let currentDaysIntoSeason; |
| if (currentSeason !== "normal") { |
| currentDaysIntoSeason = daysIntoSeason(dates[currentSeason][0]) |
| } else { |
| // Calculate how many days it has been since each season ended |
| let days = Object.keys(dates).map((str) => daysSinceDate(dates[str][1])) |
| currentDaysIntoSeason = Math.min(...days) - 1; |
| } |
| |
| return { |
| season: currentSeason, |
| filePath: filePaths[currentSeason], |
| daysIntoSeason: currentDaysIntoSeason |
| } |
| } |
| |
| const Season = async (req, res) => { |
| return res.status(200).send(getSeason()); |
| } |
| |
| export default Season; |
| export { getSeason }; |