Getting an error with Templater but the cause nor the solution is clear

I’m new to Obsidain (and love it) and installed a few plugins that I want to try out. Templater is one of them. In trying to practice with it and see what it does I copy the example code in bulk for one of it’s builtin functions and paste it into a newly created note located in the Templates folder which is configured to be where Templater looks for its templates. When I try to create a note from the template (alt+n or the equivalent from the command palette) I get the following error…

Templater Error: Invalid reference date format, try specifying one with the argument ‘reference_format’

I see there are a few issues about this on the author’s github but the content of those issues is not helpful for other users coming to the issue to find a solution to their problem.

So I go move the entire example code out of the note to paste back one line at a time (line by line) and create a note from that template with just one of the example lines at a time. All pass except 4 lines of code…

File's title date + 1 day (tomorrow): <% tp.date.now("YYYY-MM-DD", 1, tp.file.title, "YYYY-MM-DD") %> 
File's title date - 1 day (yesterday): <% tp.date.now("YYYY-MM-DD", -1, tp.file.title, "YYYY-MM-DD") %>
File's title monday: <% tp.date.weekday("YYYY-MM-DD", 0, tp.file.title, "YYYY-MM-DD") %>
File's title next monday: <% tp.date.weekday("YYYY-MM-DD", 7, tp.file.title, "YYYY-MM-DD") %>

Each of the failing lines generates the error shown shown in this post above.

I’ve looked at the Templater documentation for tp.date.now(format: string = “YYYY-MM-DD”, offset?: number⎮string, reference?: string, reference_format?: string) as well as tp.file.title. I see that each of the failing lines of code contains something not found in the other lines of code that aren’t fail tp.file.title but the cause of and the solution to my problem eludes me.

It isn’t clear from the documentation what is going on. What is the outcome and how the code works supposed to be from the execution of any one of those lines of failing code? What is missing that is causing it to fail? It isn’t clear whether the date is supposed to be inserted into the note title when using a template with that code or whether the code in the template is supposed to be getting the date format from the title of the note created by the template. In the latter case it doesn’t make sense since how do you get data from the title of a note that doesn’t exist yet? In the former case it doesn’t make sense why it wouldn’t just work the way it is written and the only thing I can ascertain is that maybe there is some other code that is missing from the example code that is needed for it to work correctly.

Can someone explain what is really going on here and what has to be done to make each of those failing lines of code work?

These 4 line of code try to extract a date from the title of the note, transforming it into a certain format. The title of the note must therefore already contain a date. It is useful for daily notes for example.

If you want to use it without the title of the note already containing a date, you first need a line of code before that changes the title of the note with a date.
Like this for example:
tp.file.rename(tp.date.now())

1 Like

@Anwen

I think I understand…

So it looks like - as the code stands now - it is only useful with inserting a template into an existing note (I found out that does work) but not the other ways of using a Templater template (eg: Templater: Open Insert Template modal or Alt+E). If additional code were to be added - such as tp.file.rename(tp.date.now()) - then I’m guessing you could use the template with the other commands to use a template (eg: Templater: Create new note from template or Alt+N).

If that is correct then it implies that Templater template content (the code) is sometimes only useful with specific ways of using the template while other template content may be suitable for all the available ways to use the content. Is that correct?

Update

Yeah

tp.file.rename(tp.date.now()
Date yesterday with format: <% tp.date.yesterday("Do MMMM YYYY") %>

…makes the template work with the other ways of using the template.

Its really good to see how the ways you use a Templater template can depend on the code you put in it.

Thanks so much for helping me get over the hump!

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