Weekly template with sub folders for year and month

What I’m trying to do

  • Inside Template/Weekly Template
  • Create a list of links to Monday, Tuesday, etc.

With
[[Journal/Daily/<%moment(tp.file.title).format(“YYYY”)%>/<%moment(tp.file.title).format(“MM”)%>/<%moment(tp.file.title).add(0,‘day’).format(“YYYY-MM-DD-dddd”)%>|Monday]]

So a file example is: Journal/Daily/2024/02/2024-02-05-Monday

But get a error about filename cannot contain x,y,z chars.
The files are stored like this.

  1. What’s wrong with the template file?
  2. How do I get access to the output error message that’s displayed in a notification box and then disappear?

/Vemund

What is the name of your weekly file where you are running this from?

Most likely your error is related to the file name having non date parts, which you don’t tell moment to look or for. In other words, when moment is looking for the date and sees “Monday” or similar, it doesn’t know what to do.

Once that is sorted out I would consider making the entire folder path from one call to moment with a more complex date format string…

Aha. So it’s the filename of the weekly file that’s input to moment?
The filename of this is: Journal/Weekly/2024-W08

image

I like this 2024-W08 file to link to
Journal/Daily/YYYY/MM/YYYY-MM-DD-dddd files. And find the filename automatically form the current week number.

It works when I dont add the YYYY/MM folder to the Journal/Daily folder.

For starters you could try with variations over this theme:

<%*
const title = tp.file.title

const mondayJournal = tp.date.now('[Journal/Daily/]YYYY/MM/YYYY-MM-DD-dddd', 0, tp.file.title, "YYYY[-W]WW")

tR += `[[${mondayJournal }|Monday]]\n\n`
_%>

## <% "[[" + mondayJournal + "|Monday]]" %>

There are a few caveats with this template:

  • It do require the tp.file.title to actually be set already, so if you rename it earlier in the template, you need to set the file name into the title variable
  • The call to tp.file.now() returns an already formatted string, not a date, so if you want to a date to iterate over to get to the rest of the week you either need to repeat code, or change it to actually get a date to manipulate through calls to moment()

Other than that the template should produce two nice links to the Monday of the given week. :smiley:

And here is a moment() based variant, where it assumes that title holds the week number, like in 2024-W08, and presents the days of the week in a list.

<%* 
const startDate = moment(title)

for (const offset of [...Array(7).keys()]) {
  const weekday = start.clone().add(offset, "day") 
 tR += `- [[Journal/Daily/${weekday.format("YYYY/MM/YYYY-MM-DD-dddd")}|${ weekday.format("dddd")}]]\n`
}
_%>

Do note that the .clone() is essential, as if you don’t use it’ll change the date and you could end up with “monday”, “wednesday”, “saturday” since we increment the offset all the time… :slight_smile: This could to some extent be circumvented by just adding one to the start, but I think it’s clearer to use the .clone() so that start remains the date for the start of the week all the time.

In my test vault with the file 2024-W08 this resulted in this markdown:

- [[Journal/Daily/2024/02/2024-02-19-Monday|Monday]]
- [[Journal/Daily/2024/02/2024-02-20-Tuesday|Tuesday]]
- [[Journal/Daily/2024/02/2024-02-21-Wednesday|Wednesday]]
- [[Journal/Daily/2024/02/2024-02-22-Thursday|Thursday]]
- [[Journal/Daily/2024/02/2024-02-23-Friday|Friday]]
- [[Journal/Daily/2024/02/2024-02-24-Saturday|Saturday]]
- [[Journal/Daily/2024/02/2024-02-25-Sunday|Sunday]]

All fully qualified links which should be created in the correct sub folder.

1 Like

It’s probably an JS issue. But I dont get the script to exec? How? What Obsidian option do I need to enable. See gif.
ObTempExecDontwork

Get an error when I run Templater replace… Alt-R

You need to paste it with Shift+Cmd+V (or Ctrl instead of Cmd if on windows/ linux) to preserve the line shifts.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.