Group content by week number of year

I want to summarize my posts weekly, so I want to write a archetype template to auto find out pages of latest week and put into a new pages.

I found this may be useful: https://gohugo.io/templates/lists/#by-date-1 but I didn’t found the right format of week number of date.

I have tried below code according to https://golang.org/pkg/time/#Time.ISOWeek return format.

{{ range .Site.Pages.GroupByDate "2006 50" }}
## {{ .Key }}
{{ end }}

But won’t work.

I tried to code an example to this but couldn’t. Hugo simply doesn’t recognise weeknumbers (as you already pointed out), which of course makes sense because based on the source I looked at, Go doesn’t either.

There’s still a workaround, however. If you add the weeknumber to the front matter of each post, you can use that value as a key to group by. Of course, that’s not great since it’s a hassle to figure out the weeknumber and to ensure you always provide it in a post.

Sorry for not being more helpful. :hugs:

How about calculating week start/end dates in an array and walking through that, comparing against publishdate?

You might have to create the list of dates separately but I’m sure there are lots of tools that could create a JSON data file for you.

Thanks for trying. I thought about add weeknumber to front matter, but it’s not convenience, neither organize manually, I don’t have to deal with lots of files each week for now, so I decided to organize by hand now.

Hi, in 2026, any suggestion on this? AI continuously told me the wrong method of `ISOWeek`, I’m frustrated on the fact that no way to get a exact year week in Hugo?? I think i must be wrong as it’s insane

UPDATE: Gemini give me the implementation after I told it a stupid brute-force solution. It says there is a algorithm, named “Nearest Thursday”.

The Algorithm

  • Convert the day to an ISO Weekday (Monday=1 … Sunday=7).
  • Find the Thursday of that week (add/subtract days).
  • The ISO Year and ISO Week are determined by that Thursday’s Year and YearDay.

Here is the final shortcodes you can use in the partials.

{{/*

File: layouts/partials/func/GetISOWeek.html

Usage: {{ partial "func/GetISOWeek.html" .Date }}

Returns: String (e.g., "2023-W05")

*/}}




{{- $date := . -}}




{{/* 1. Normalize Weekday (Mon=1 ... Sun=7) */}}

{{- $isoDay := cond (eq $date.Weekday 0) 7 (int $date.Weekday) -}}




{{/* 2. Find Thursday (ISO Week belongs to the year of its Thursday) */}}

{{- $offset := sub 4 $isoDay -}}

{{- $thursday := $date.AddDate 0 0 $offset -}}




{{/* 3. Calculate Week Number */}}

{{- $isoYear := $thursday.Year -}}

{{- $isoWeek := add (div (sub $thursday.YearDay 1) 7) 1 -}}




{{/* 4. Return the formatted string */}}

{{- return printf "%04d-W%02d" $isoYear $isoWeek -}}

Interesting!

I suggest this, as partial func/getIsoWeek:

{{- $date := time.AsTime . -}}
{{- $weekday := $date.Weekday }}
{{/* 1. Normalize Weekday (Mon=1 ... Sun=7) */}}
{{- $isoDay := cond (eq $weekday 0) 7 (int $weekday) -}}
{{/* 2. Find Thursday (ISO Week belongs to the year of its Thursday) */}}
{{- $offset := sub 4 $isoDay -}}
{{- $thursday := $date.AddDate 0 0 $offset -}}
{{/* 3. Calculate Week Number */}}
{{- $isoYear := $thursday.Year -}}
{{- $isoWeek := add (div (sub $thursday.YearDay 1) 7) 1 -}}
{{/* 4. Return the formatted string */}}
{{- $data := dict "week" $isoWeek "year" $isoYear -}}
{{- return $data -}}

And if you need an ouput like you, use partial utils/formatIsoDate:

{{- $data := partial "func/getIsoWeek" . -}}
{{- $s := printf "w%02d-y%04d" $data.week $data.year -}}
{{- return $s -}}

This works both when calling the default file in Archetype and throughout the rest of the site…
but maybe I’m wrong!? :wink:

as instance:

  • {{ partial "utils/formatIsoDate" .Date }}
  • or {{ partial "utils/formatIsoDate" now }}