Replies: 1 comment
-
I'm not opposed to adding a utility function for this to the library, but in the meantime it should be fairly simple to do yourself: function getWeekNumber(date, locale) {
let weekStart = startOfWeek(date, locale);
let yearStart = startOfWeek(startOfYear(date), locale);
let dayOfYear = date.calendar.toJulianDay(weekStart) - yearStart.calendar.toJulianDay(yearStart);
return dayOfYear / 7;
}
getWeekNumber(new CalendarDate(2023, 2, 3), 'en-US') // => 4 This uses the Julian day to calculate the number of days between the start of the week for the given day to the start of the week for the first day of the year. Dividing that by 7 gives you the number of weeks. Stackblitz where you can play with it. |
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
-
It would be helpful to provide some utility for getting the week numbers in a month, or alternatively the week number for a date. I want to display the week numbers next to the dates in our calendar component, however, there doesn't seem to be a good way of calculating this at the moment.
Proposed API
Alternative 1 -
getWeekNumbersInMonth
This function would be an alternative to
getWeeksInMonth
, however instead of returning the number of weeks as a number it would return an array of week numbers, like the following:This would make it slightly easier to build the calendar grid as you could simply iterate over the week numbers to get the correct number of rows.
One potential issue with this approach is that people might get confused when later having to pass week index to the
state.getDatesInWeek
function.Alternative 2 -
state.getWeekNumber
This alternative would likely not lead to the same confusion as with alternative 1, where people might pass the wrong values to
state.getDatesInWeek
function.Beta Was this translation helpful? Give feedback.
All reactions