Getting something like {{date}} with Templater

I’ve been enjoying the nice aspects of working with the Templater plugin for handling dates in templates. However, one thing that I’m missing in Templater is an equivalent of the built-in {{date}} template.

I tend to choose future days and weeks with the Calendar plugin, and I want the templates to have certain dates be inserted, relative to the chosen day or chosen week. With {{date}} I only got one date, but I could manually change the dates after the template being applied. With Templater, I can’t do {{date}}, becuase it seems to be treated as some kind of literal.

For daily notes one can probably use tp.file.title, but this does not quite work for weekly notes, as they do not have a full date in the file name to work with.

Does anyone have any ideas for how this could be solved?

Well, I don’t know about the Templater plugin, but I did notice {{date}} is mentioned in the latest Insider release about an hour after you posted. Obsidian Release v0.12.16 (Insider build)

So there’s a chance it might be relevant to you?

How we can download latest version ?

1 Like

At the moment it’s available to Catalyst (and up) subscriptions first. It will be released to all after a period of time.

You can use offsetted dates from the current date, though yeah you’re right that Templater wouldn’t understand {{date}} in your templates.

tp.date.now() has an offset property as its second parameter, the number of days offsetted from the current date.

For example, you can use tp.date.now("YYYY-MM-DD", 7) to get the date a week from now.

I don’t know if this will do the trick, as I’m not yet a subscriber. I’ll have to wait for v0.12.16 to be released.

My suspicion is that it will not work, as I was already using the {{date:YYYY-MM-DD}} format, and that was entered as a literal.

Is {{date}} maybe exposed as some Obsidian API variable?

I remember that Templater could access Obsidian API calls, but I’m not sure if this would be one of the cases where that’s useful.

After experimenting a bit, what I’ve changed my setup to do is to have the templates contain {{date}}, as these are applied correctly when creating a new note (or when invoking the regular “Templates” plugin). Since Templater is set up (with a configuration option) to evaluate its variables upon creating a new note, the values are replaced, which can result in the correct values being input (if tp.date.now equals to the current date).

This still does not really solve the problem, but by using {{date}} in the right places and using some <% tp.file.cursor() %> to move the cursor to the various places where dates may be wrong, the solution is decent.

Still hoping that someone here knows a way to have Templater access the {{date}} value.

If I understood your situation correctly, you’re using the calendar plugin to create future daily notes and want to do date math relative to that daily note. You can do this in Templater with the date.now function.

<% tp.date.now(“YYYY-MM-DD”, -1, tp.file.title, “YYYY-MM-DD_ddd”) %>

So, what this function does is output date in YYYY-MM-DD format by looking at the note title (I believe) which is in the YYYY-MM-DD_ddd format. The -1 gives you the prior day (e.g. for daily note 2021-09-21_Tue this will output 2021-09-20). You should be able to change the date formats in both places as you please as long as you capture the month, day, and year in the note title. You can change the offet to whatever you need: -1 for the day before, 0 for that day, 1 for the day after, etc.

You can have this output week numbers using the ww date format, but I couldn’t find a simple way to do date math based purely on future weekly notes.

Right, this was my current understanding of the capabilities.

tp.file.title would work if the title is a daily note, while week notes (or month notes, which I currently don’t use) would not work.

The best thing (apart from actually somehow obtaining the {{date}} value directly) would be to be able to have {{date}} be evaluated first by the built-in templating system, take that text and have Templater interpret that value, and have this interpreted date value be stored somewhere as a variable for further use. I’m guessing that this is not something that’s possible.

This sounds like something that could be made possible within the Templater plugin itself. Have you tried posting an issue on the repo? I skimmed through a few of the issues for {{date}} but didn’t see anything like what you’re after.

I have not yet posted an issue there, no. I may indeed have to do that, for some parts of what I’m looking for.

1 Like

Today I noticed the following section in the Obsidian Tasks plugin documentation:

While it’s not Templater, the Periodic Notes plugin is providing a {{date+Xd}} format, for doing relative days.

This covers most of my needs. I suspect that the only thing left is to somehow inject the result of a these {{date+Xd}} evaluations as a variable, as using the result as a reference date would enable the last pieces of missing functionality (e.g. usage of the weekday function, where I can currently only use today’s date as the reference date).

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