Skip to content

Instantly share code, notes, and snippets.

@jkohlin
Created December 19, 2022 09:52
Show Gist options
  • Save jkohlin/ce0381cd09cc24f692fba9f360d6bec2 to your computer and use it in GitHub Desktop.
Save jkohlin/ce0381cd09cc24f692fba9f360d6bec2 to your computer and use it in GitHub Desktop.
Get the start date and end date from a weekString ('2021-W01')
/**
* <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