Created
January 11, 2016 11:21
-
-
Save xonlly/601ecb1cd7c90946b412 to your computer and use it in GitHub Desktop.
Get first and last day by a week number.
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
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