Skip to content

Instantly share code, notes, and snippets.

@xonlly
Created January 11, 2016 11:21
Show Gist options
  • Save xonlly/601ecb1cd7c90946b412 to your computer and use it in GitHub Desktop.
Save xonlly/601ecb1cd7c90946b412 to your computer and use it in GitHub Desktop.
Get first and last day by a week number.
function weekToDate(year, wn, dayNb){
var j10 = new Date( year,0,10,12,0,0),
j4 = new Date( year,0,4,12,0,0),
mon1 = j4.getTime() - j10.getDay() * 86400000;
return [
new Date(mon1 + ((wn - 2) * 7 + dayNb) * 86400000),
new Date(mon1 + ((wn - 1) * 7 + dayNb-1) * 86400000),
]
};
// Fr exemple
console.log(w2date(2016, 3, 0))
// Us
console.log(w2date(2016, 3, 1))
// return
Array [ Date 2016-01-11T11:00:00.000Z, Date 2016-01-17T11:00:00.000Z ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment