How to loop over datetime?

Say i want to make a quick planner via a grid and i want to automate entering the date for each day, i understand that using a for loop just puts everything in a single cell but i don’t understand the spread function too well.

#grid(columns: 7), align: horizon, gutter: 0.5em,
  for i in range(start, stop + 1) [#datetime(
      year: 2025,
      month: month,
      day: i,
    ).display("[day]/[month]")])

I tried something like this but i get a single cell but i can’t seem to find a quick solution. Can someone help?

1 Like

Hi, welcome to the Typst forum!

Spreading here will only work if you have an array to spread, and your for loop generates a single content value (the loop result is obtained by joining the values of all steps, and here they produce content and joining content gives a content value).

One solution is to make sure that each step produces an array (with a single value):

#table(
  columns: 7,
  ..for i in range(1, 4) {
    (datetime(year: 2025, month: 3, day: i, ).display("[day]/[month]"),)
  },
)

This works because when arrays are joined you get an array with all the elements. But it’s a bit clunky. Better use map on the range array to transform each range value into the corresponding date string:

#table(
  columns: 7,
  ..range(1, 4).map(
    i => datetime(year: 2025, month: 3, day: i).display("[day]/[month]")
  )
)
6 Likes
1 Like