Templater plugin (1.5 and newer): New Syntax and Examples

Thanks for this discovery! This should be in the documentation already. I was looking for this since the day dynamic commands were released but in my attempts to try this, it never worked, I think I tried the asterisk before the plus and then tried the documentation and gave up. This could be so useful especially since we can call external APIs using fetch inside the template and process it as we like with JS.

Life is full of possibilities!

3 Likes

In case it’s helpful, here’s syntax for adding next/back date links to your daily note based on the note title – not today’s date:

<< [[<% tp.date.now("YYYY-MM-DD",-1,tp.date.now("YYYY-MM-DD", 0, tp.file.title, "YYYY-MM-DD"),"YYYY-MM-DD") %>]] | [[<% tp.date.now("YYYY-MM-DD",1,tp.date.now("YYYY-MM-DD", 0, tp.file.title, "YYYY-MM-DD"),"YYYY-MM-DD") %>]] >>

This assumes files are named in YYYY-MM-DD format; you’ll need to update all 8 date format declarations if you use a different format.

5 Likes

When I try to do the “Days until Christmas” example. I get “PT0.219S.” What am I doing wrong? It’s been years since I’ve done anything like this.

4 Likes

Hm. Thanks for bringing this up!

Something must have changed, or I made a stupid mistake. I also get PT0.219S (ISO duration, meaning 0.219 seconds) where it should be 219 days.

At least

<% moment("12-24", "MM-DD").diff(moment(), "days") %>

shows 219 (the correct # days). Which should be enough for the intended “days til Xmas”.

If, for some reason, it needs to be more exact, one could probably use

<% moment.duration(moment("12-24", "MM-DD").diff(moment())).as('days') %>

to arrive at 219.278469375 (at this moment in time).

Sorry if I messed something up!

4 Likes

That totally worked.

…can I ask you an off-topic question because I can’t for the life of me figure out how to search for “u”…is {{date:u}} just impossible?

1 Like

Sounds like PHP, right? u is the timestamp in integer microseconds, right? I don’t think moment.js ever supported that.

In case of using (PHP) v (milliseconds), this is x with moment.js.

u is the number day of the week, with 1 being Monday.

it’s not vital, I’m just trying to get something to link to files in the format I had already used. I really appreciate you trying to help!

I just like interesting problems :wink:

Day-of-the-week could be either d, e or E, according to the docs. For today, they all show 2 here:

<% moment().format('d - e - E') %>

You probably want E which returns 1–7 for Mon–Sun.

2 Likes

E worked! you are fantastic. :heart_eyes:

2 Likes

Happy to help :slight_smile:

1 Like

While we’re at it …

Days until …, done right

This is a little longer, but it will check if it’s already past the date for this year, and show the difference in days until the same date next year!

This is ideal for recurring events like Christmas, Birthdays, Anniversaries, etc.

To make things simple, I use JS to set a variable at the beginning. This allows editing just one place instead of many.

<!-- Days until any "MM-DD" date this year/next year -->
<%+* let edate = moment("12-24", "MM-DD"); let from = moment().startOf('day'); edate.diff(from, "days") >= 0 ? tR += edate.diff(from, "days") : tR += edate.add(1, "year").diff(from, "days") %>

Days until …, plus “It’s Today!”

Same as above, but will display “It’s today!” instead of a plain old zero if we happen to be on the date.

<!-- Days until any "MM-DD" date this year/next year with "TODAY" -->
<%+* let edate = moment("12-24", "MM-DD"); let from = moment().startOf('day'); edate.diff(from, "days") >= 0 ? edate.diff(from, "days") == 0 ? tR += "It’s today!" : tR += edate.diff(from, "days") : tR += edate.add(1, "year").diff(from, "days") %>

Enjoy! (And never again worry about “days to Christmas” being -2!)

4 Likes

Caveat: Using moment() in day-only calculations → Wrong results!

A moment() always includes the time, so it’s more like a now() than like a today()!

Partial “moments” (like those gotten from, say moment("2021-05-23")) always get their time set to 00:00:00 local time.

Thus, when calculating “days to some date” you will get wrong results, especially when “now” is late in the day!

This is a trap I also often fell into.

The easy cure—if doing something with whole days, i.e. when not needing the time—is to always use moment().startOf('day') in these cases. This will also set the time part to 00:00:00 local time, and thus make things calculate correctly again!

Great to see this thread getting momentum—keep ’em coming!

6 Likes

A bit late but yes, you’re absolutely right here concerning the use of Moment() :blush:, I should have been more precise in my explanations :innocent: !

Thank you for clearing this out :blush: !

1 Like

I’m building a template for monthly notes (which will be created from the Periodic Notes plugin. I would like the template to include a link to the next month and the previous month, and I’d like the template to already calculate the next month based off of the month of the template instance.

For example, if I use the template to create a note for [[2021-08]], then I want there to be a note for [[2021-07]] (last month) and a note for [[2021-09]] (next month).

I don’t want it to be calculated based off of the date for today tp.date.now(). I want it to be based off of whatever the month for that monthly note is.

Does anyone know how I would build something like this?

I just started to use Templater and I am really liking it. Apparently all my old built in template dates fell apart since an update.

I have watching YouTube Tutorials on how to use Templater from the likes of Bryan Jenks and Productivity Guru.

I am wishing for a daysInYear() for moment. I really would like to set up a thing that outputs:

  • Day 68 out of 365, or a shorthand 68/365
  • In a leap year:
  • Day 68 out of 366 or 68/365

It has been about 14 years since I have done any java programming, so please excuse my bad attempt of showing code.

I am guessing one could use an if then else statement to do this instead of my complicated thinking, with a variable for year, and then a variable for the return value if the statement comes back true then the moment format would have the constant of 366, else it prints out the moment format with constant of 365.

So something like:
varA = YYYY
If moment(“varA”,”YYYY”).isLeapYear() is true
Then moment().format(“[Day] DDDD [of 366]”)
Else
moment().format(“[Day] DDDD [of 365]”)

Some of these coding examples are excellent, and I will be snagging some to use for days until vacation, etc.

Oh and a lot of Danes use a local format for date as: of dd/mm - YYYY

For the number of days in the current year (for example), you can use:

<%+ moment("12-31", "MM-DD").dayOfYear() %>

This will return the day number of December 31st in the current year, which is the last day in a year, and thus the same as the number of days in the year. :wink:

What is the + after the <%t+ moment code you wrote? I didn’t see your reply before I edited my post.

The + after <% tells Templater it’s a Dynamic Command, i.e., freshly evaluated everytime.

So your new example above could be written either as

<% moment().format("[Day] DDDD of " + moment("12-31", "MM-DD").dayOfYear()) %>

(static), or

<%+ moment().format("[Day] DDDD of " + moment("12-31", "MM-DD").dayOfYear()) %>

(dynamic, for automatic updating).

2 Likes

You answered my question before I could formulate it and type it out. I was going to ask about nesting functions such as moment.

Thank you!

Glad if it helped you! :slight_smile: