Traversing Weekly Notes with Templater

Result

<< [[2022-W30]] | [[2022-W32]] >>

Implementation

If you create Weekly Notes on Sunday, the template should have the following structure:

<< [[<% tp.date.now("YYYY") %>-W<% tp.date.now("WW") - -1%>]] | [[<% tp.date.now("YYYY") %>-W<% tp.date.now("WW") - -3 %>]] >>

If you create Weekly Notes on Monday, the template should have the following structure:

<< [[<% tp.date.now("YYYY") %>-W<% tp.date.now("WW") %>]] | [[<% tp.date.now("YYYY") %>-W<% tp.date.now("WW") - -2 %>]] >>

Why?

As far as I can tell, tp.date.now("WW") changes on Monday. Posts like this seem to imply that moment.js (the library that powers tp.date) also thinks weeks start on Sunday and end Saturday, but in my own testing with .now() that does not seem to be the case.

Sunday β†’ tp.date.now("WW") = 29

Tuesday (i forgot to run this on Monday, sue me) β†’ tp.date.now("WW") = 30

In addition, templater ignores β€œ+” meaning you have to subtract negative numbers to get the desired result, which explains the β€œ- -1” and β€œ- -3” above.

Summary

  • Sunday and Monday require different templates
  • Don’t not use double negatives to get weeks in the future
1 Like

The ISO week numbering starts on Mondays. I’m assuming that is where the different weeks between Sunday and Monday is coming from.

From Wikipedia:

ISO week date

1 Like

Great catch! I wonder why the moment.js devs decided to use .startOfWeek() as Sunday instead of Monday

Edit: Yep seems to be the case

For all years, 8 days have a fixed ISO week number (between W01 and W08) in January and February. With the exception of leap years starting on Thursday, dates with fixed week numbers occur in all months of the year (for 1 day of each ISO week W01 to W52).
– ISO week date - Wikipedia

There are different format syntaxes for ISO week vs non-ISO week (commonly used in US and Canada). WW (capitalized) is ISO; ww (lowercase) is non-ISO. There are also some locale-specific format options - startOfWeek should be based on your locale unless some plugin has changed that locale.

1 Like