Get Start of Week by ISO Week Number #2168
Unanswered
taylorharwin
asked this question in
Q&A
Replies: 1 comment
-
Hi @taylorharwin, You could solve this using import { parse } from "date-fns";
const date = parse("2 2021", "I R", new Date());
// Mon Jan 11 2021 00:00:00 GMT-0500 (Eastern Standard Time) Here's an alternative, which I think would be lighter in terms of filesize comparatively to import { setISOWeek, startOfISOWeekYear } from "date-fns";
// careful! pass any date in the correct year to `startOfISOWeekYear`
// but not too close to Jan 1st because it might give you the start of the previous ISO year.
// Early january dates can be part of the last ISO week of the previous year.
const date = setISOWeek(startOfISOWeekYear(new Date(2021, 2, 1)), 2)
// Mon Jan 11 2021 00:00:00 GMT-0500 (Eastern Standard Time) Then just format the date as needed: format(date, "PPP");
// January 11th, 2021
format(date, 'MMMM d, yyyy')
// January 11, 2021 Hope this is helpful! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
How would I convert from ISO Week and Year numbers to a Date representing the start of a week? I see functions to do the reverse (getISOWeek). I'd like to go from this:
{week: 2, year: 2021}
to
"Week of January 11, 2021"
The options to do this on Stack Overflow all seemed very out of date 🥁
Beta Was this translation helpful? Give feedback.
All reactions