How do I create a list of the last 100 years using the current year?

I’m trying to output a list of dates in reverse order to populate an input box. My Go knowledge isn’t that great so I’m quite sure how to get this to work:

  <select class="select" name="birthday_year">
   {{ range sort $s := (seq 1905 (now.Format "2006")) }}
       <option>$s</option>
    {{ end }}
  </select>

Ideally I’d like to get the last 100 years from the current year but I can’t get the sequence to even ouput in its current form.

Hi,

This outputs a string, which is probably why seq isn’t working. You could wrap it in an int cast.ToInt | Hugo

and then sub 2018 100

So you end up with something like:


{{ range $num := seq (int (now.Format "2006")) (sub (int (now.Format "2006")) 100) }}
     {{$num}}
{{ end }}

I think now.Year will give you an int with the current year.

2 Likes

Thanks guys. I really need to get a grasp of Go. Loving Hugo so much, but Go feels odd compared to the languages I know.