How to perform "date-math" in typst?

108 views Asked by At

I want to always get either today's date (assuming today is a friday) or the next week's friday. How would I go about implementing the same mechanic in Typst? Within the documentation I have not yet found a way to perform "date-math" like shown above.

In Python I could do something like this:

report_date = date.today() + timedelta((5 - date.today().isoweekday()) % 7)

In Typst I can (seemingly) only elegantly cover the "today is friday" branch:

#let report_date = if datetime.today().weekday() == 5 {
  datetime.today().display("[month repr:long] [day] [year]")
} else {
  ... # TODO
}
1

There are 1 answers

1
xkevio On BEST ANSWER

See the Duration type for performing date arithmetic: Duration Type. You can more or less 1:1 translate your Python snippet like this:

#let report-date = datetime.today() + duration(days: calc.rem(5 - datetime.today().weekday(), 7))