feat: dayjs().startOf(yearisoweek) #1473 #1474
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Describe the bug
This is NOT a bug, it's just a feature i wish dayjs to have.
Expected behavior
So my project requires to translate between an ISO8601 date(year week weekday) and an actual Date object.
I found dayjs can translate Date to ISO8601 easily by using isoWeek and isoWeekday, but it does not support for the reverse action.
i tried with dayjs('2021', 'YYYY').isoWeek(1), but it returns the first isoweek of 2020 :(
So i added a new function under dayjs().startOf(), named "yearisoweek" (idk what it should be called..) to calculate the first isoWeek and isoWeekday for a given date (eg. dayjs('2021-04-25', 'YYYY-MM-DD).startOf('yearisoweek) == dayjs('2021-01-04, 'YYYY-MM-DD')), so it gives 2021W011. After it founds the first isoWeek and first isoWeekday of this year, it should be easy to calculate other ISO8601 dates by using the dayjs.add(week, 'week') and dayjs().add(day, 'day') functions.
In the test case, i found my method is not included in moment.js, it cannot be work with the startOf test in the badMutable.test.js, so i put them i isoWeek.test.js, under startOf yearisoweek
again the name is crap..cant figure a better one atm.
this is my first time doing a pr request, let me know where i could improve, thanks!
hope this could help you guys xD
Information
Day.js Version [v1.10.4]
OS: [macOS]
Browser [nodejs]
Time zone: [UTC+08:00]