Skip to content

Instantly share code, notes, and snippets.

@pignuante
Last active April 19, 2020 05:05
Show Gist options
  • Save pignuante/ca17258ac4bde1251eedec02adacd0e0 to your computer and use it in GitHub Desktop.
Save pignuante/ca17258ac4bde1251eedec02adacd0e0 to your computer and use it in GitHub Desktop.
function getWeekOfMonth(date) {
let adjustedDate = date.getDate()+date.getDay();
let prefixes = ['0', '1', '2', '3', '4', '5'];
return (parseInt(prefixes[0 | adjustedDate / 7])+1);
}
function getDateOfWeek(w, y) {
var d = (1 + (w - 1) * 7); // 1st of January + 7 days for each week
return new Date(y, 0, d);
}
function getDateOfISOWeek(w, y) {
var simple = new Date(y, 0, 1 + (w - 1) * 7);
var dow = simple.getDay();
var ISOweekStart = simple;
if (dow <= 4)
ISOweekStart.setDate(simple.getDate() - simple.getDay() + 1);
else
ISOweekStart.setDate(simple.getDate() + 8 - simple.getDay());
return ISOweekStart;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment