How can I calculate a week number

Im trying to calculate the week number in a month.

i.e as of posting this, its the 5th week in July.

{{ now().isocalendar()[1] }}

or

{{ now().strftime("%W") }}

works ok for the week number in the year but can’t figure out how to get the week in the current month.

This should get you what you want.

{% set adjusted = now().day + now().replace(day=1).weekday() %}
{{ (adjusted / 7) | round(0, 'ceil') }}

That is awesome, thanks Petro. I will work my way through that and try to understand how its works :+1:

now().day gets the current day today.
now().replace(day=1).weekday() gets the first day of the month, as a weekday. For example, if the month started on wednesday, this would return 2, 0 being monday, 1 being tuesday 3 being wednesday. We need to add this value to the current days to properly get the number of weeks.
set adjusted = now().day + now().replace(day=1).weekday() adds the 2 values and places the result in a variable
(adjusted / 7) | round(0, 'ceil') divides the result by 7 and rounds it up to the whole number.

1 Like

Thats brilliant, I was just trying to understand the

now().replace(day=1).weekday()

thanks again.