Created
March 11, 2023 14:03
-
-
Save dnnsmnstrr/6697bef89a52a6114a601dd1157e781c to your computer and use it in GitHub Desktop.
Helper to figure out the schedule for who was responsible to bring out the trash in which week
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const parties = [ | |
'Herr Freitag', | |
'Frau Samstag', | |
'WG' | |
] | |
console.log(parties.length) | |
const START_WEEK = 6 | |
const getWeekNumber = (date = new Date()) => { | |
// Copy date so don't modify original | |
const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate())); | |
// Set to nearest Thursday: current date + 4 - current day number | |
// Make Sunday's day number 7 | |
d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7)); | |
// Get first day of year | |
var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1)); | |
// Calculate full weeks to nearest Thursday | |
var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7); | |
// Return array of year and week number | |
return weekNo | |
} | |
const getResponsibleParty = (parties = ['you'], weekNo = 1) => { | |
console.log('weekNo', weekNo) | |
const responsibleIndex = Math.floor((weekNo + START_WEEK) / parties.length) - 1 | |
if (responsibleIndex < 0) { | |
return parties.shift() | |
} | |
console.log('responsibleIndex', responsibleIndex) | |
return parties[responsibleIndex] | |
} | |
const wasResponsible = getResponsibleParty(parties, 19) | |
console.log('wasResponsible', wasResponsible) | |
const currentlyResponsible = getResponsibleParty(parties, getWeekNumber()) | |
console.log('Currently responsible: ', currentlyResponsible) | |
const nextResponsible = getResponsibleParty(parties, getWeekNumber() + 1) | |
console.log('nextResponsible', nextResponsible) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment