Skip to content

Instantly share code, notes, and snippets.

@gkucmierz
Last active February 3, 2021 14:20
Show Gist options
  • Save gkucmierz/a84bb148a9b0c1d6be99f1d89e08e6e3 to your computer and use it in GitHub Desktop.
Save gkucmierz/a84bb148a9b0c1d6be99f1d89e08e6e3 to your computer and use it in GitHub Desktop.
// const Sunday = 0;
// const Monday = 1;
const TUESDAY = 2;
// const Wednesday = 3;
// const Thursday = 4;
// const Friday = 5;
// const Saturday = 6;
const firstDayOfMonth = (year, month, weekDay) => {
const DAYS_IN_WEEK = 7;
const day = new Date(year, month, 0).getDay();
const shift = (DAYS_IN_WEEK + weekDay - day) % DAYS_IN_WEEK;
const date = new Date(year, month, shift);
return date;
};
console.log('luty 2021', firstDayOfMonth(2021, 1, TUESDAY).toString());
console.log('styczeń 2021', firstDayOfMonth(2021, 0, TUESDAY).toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment