-
Notifications
You must be signed in to change notification settings - Fork 155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
From week number to start and end timestamp #2018
Comments
Hi @nicolasr75 - Below is one way to get the date range you're looking for. The trick is that the fourth of January is always on the first ISO week of the year. So to calculate a week range, just find the start of the week that contains Jan 4, and add the correct number of weeks.
EDIT 2022-01-28: fixed a bug in code above as discussed in later comments |
Thanks, I see. Would be great to see that example in the cookbook or somewhere else. Not everyone will think of this immediately ;-) |
Would you be able to mention a bit more about your use case in js-temporal/proposal-temporal-v2#11 ? |
@ptomato Done! |
@justingrant Your example does not work for week number 1 in 2022 because
throws an error "mixed-sign values not allowed as duration fields". That is because
|
@nicolasr75 - Thanks for catching that! Note that week number 1 is OK because zeroes are always allowed. It's only mixing positive and negative that fails. So week number 2+ will fail, e.g. I updated the code in my original comment so that folks finding this in Google won't copy/paste the bug. |
I think the question has been answered, so I'll close this. |
A common scenario in my business domain is that users select a calendar week by selecting the week number and the year. The software is supposed to convert this into the start and end timestamps for the given week. An example would be a query for all measured temperature values of a specific week.
Temporal knows weekOfYear and dayOfWeek but how do I go the other direction?
Given an ISO calendar week number and a year, how do we get to the start and end date of that week?
moment.js allows me to set the week, see f.e. here:
https://www.tutorialspoint.com/momentjs/momentjs_week_of_year.htm
The text was updated successfully, but these errors were encountered: