Created
December 19, 2022 09:52
-
-
Save jkohlin/ce0381cd09cc24f692fba9f360d6bec2 to your computer and use it in GitHub Desktop.
Get the start date and end date from a weekString ('2021-W01')
This file contains hidden or 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
/** | |
* <input type=week genererar en sträng typ '2022-W12' som vi vill ha omvandlad till två datum | |
* @param {string} weekString:'2021-W01' | |
* @returns {object} {from: date, to: date} | |
*/ | |
export const datesFromWeek = (weekString) => { | |
let [y, w] = weekString.split('-') | |
w = w.replace('W', '') | |
const offsetDate = new Date(y, 0, w * 7) | |
return { | |
from: new Date(offsetDate.setDate(offsetDate.getDate() - offsetDate.getDay() + 1)), | |
to: new Date(offsetDate.setDate(offsetDate.getDate() + 7 - offsetDate.getDay())) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment