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

In case anyone else needs to do this, @shabegom suggested this answer which works great:

const currentMonthDates = new Array(moment(tp.file.title,'YYYY-MM').endOf('month').week() - moment(tp.file.title, 'YYYY-MM').startOf('month').week() + 1).fill(null).map((x, i) => moment(tp.file.title,'YYYY-MM').startOf('month').add(i, 'weeks').format("- ![[gggg-[W]ww[]]]")).join('\r\n') %>
<%* tR +=  currentMonthDates %>

Hi all,

Is it possible to create a time-blocking list of 15 minute increments, something like;

e.g. if the current time is 08:51, I might want to start my time blocking at 09:00

- <% tp.date.now("HH:mm", +x min) %>
- <% tp.date.now("HH:mm", +15 min) %>
- <% tp.date.now("HH:mm", +30 min) %>
- <% tp.date.now("HH:mm", +45 min) %>
- <% tp.date.now("HH:mm", +60 min) %>

one would just need to insert the adjustment time x, and it would be a nice little focus prompt alongside a pomodoro timer.

thanks!

Hey @dryice! I’ve actually once written two complete moment.js addons for rounding date/time and generating date/time intervals. These are a little more complicated and not so easy to add to Obsidian, so here are two simpler solutions, maybe one of these fits your bill:

Very simple (and ugly) solution without Javascript:

- <% moment().add(15-(moment().minute()%15),'minutes').startOf('minute').format('HH:mm') %>
- <% moment().add(15-(moment().minute()%15),'minutes').startOf('minute').add(15, 'minutes').format('HH:mm') %>
- <% moment().add(15-(moment().minute()%15),'minutes').startOf('minute').add(30, 'minutes').format('HH:mm') %>

Simple solution using Javascript:

<%*
let start = moment().add(15-(moment().minute()%15),'minutes').startOf('minute');
tR += "- " + start.format('HH:mm') + "  \n";
// moment.js dates are mutable, so always only add 15 mins!
tR += "- " + start.add(15, 'minutes').format('HH:mm') + "  \n";
tR += "- " + start.add(15, 'minutes').format('HH:mm') + "  \n";
%>

It is assumed that both of these are saved as a Templater template in the template folder, and inserted upon request (Alt+E).

Result (for both) should be (at 19:58:17):

  • 20:00
  • 20:15
  • 20:30
1 Like

Many thanks, both solutions work perfectly, and yes, the second is nice and tidy! :ok_hand:
Have a lovely weekend!

1 Like

Hello @Moonbase59 – I’m reading through this read and this is a wealth of knowledge. You’re a true tinkerer. I’m running into trouble and wanted to see if you saw something I was doing simply wrong or if you knew of a smart way to achieve this.

I’m looking to dynamically link all the months in a quarter, depending on the quarter. As such, I was planning on writing something like:

 <%* if (tp.date.quarter() == "1") { %>
- 2021-1
- 2021-2
- etc
<%* } %>

However, I’m getting Templater Error: Template parsing error, aborting. tp.date.quarter is not a function.

I’ve also tried to use tp.date.now().quarter(), but get a similar error.

Am I doing something wrong? I thought that Templater exposed the entire Moment object and methods?

Thanks in advance for your help!

@ScottKillen has a terrific “if-else if” example using the Templater plugin that may be able to help you. use-the-templater-plugin-to-create-repeating-tasks-on-future-or-past-daily-notes

1 Like

@musa Try moment().format("Q")

Dynamic Commands
Why would:

<%+ tp.file.last_modified_date("YYYY-MM-DD HH:mm:ss") %>

not work in frontmatter but in the body of a note it works perfectly well?

Did you ever figure out how to get the months in the quarter to work?

1 Like

With below template

This week's monday: <% tp.date.weekday("dddd, Do MMMM YYYY", 0) %>
Next monday: <% tp.date.weekday("dddd, Do MMMM YYYY", 7) %>

I am getting output

This week's monday: Sunday, 3rd April 2022
Next monday: Sunday, 10th April 2022

In windows settings, Monday is first week of day.
Templater version 1.12

Done some digging and found that, if we change locale in “About” setting of obsidian based on that moment.js decides weekday will be Sunday or Monday.
I wanted Monday and Locale as “English”.

One work around I found is Calendar plug-in has setting Start week on, this also changes behavior of moment.js.

After changing start week on, in calendar setting

<% tp.date.weekday("dddd, Do MMMM YYYY", 0) %>

gives Monday as first day of week.

2 Likes

Hi,

Thanks for the plugin.

How can I display just the time? Struggling with a seemingly simple code lol… but couldnt find it in the documentation.

How do I make my templater command for my daily note using the tp.file.title return the following Wednesday, Apr 20th

<% tp.date.now(“dddd, MMM Do”, 0, tp.file.title, “YYYY-MM-DD”) %>

I was able to figure it out finally. Just to clarify it calculates the days of the week given a title like 2022-W20 and links it to the daily notes for that week, which I have formatted as YYYY-MM-DD

<% a = new Array(moment(tp.file.title,'YYYY-[W]ww').endOf('week').day() -moment(tp.file.title,'YYYY-[W]ww').startOf('week').day()+1).fill(null).
map((x, i) => moment(tp.file.title,'YYYY-[W]ww').startOf('week').add(i, 'days').format("- [[[] YYYY-MM-DD [|] ddd [⦗]YYYY-MM-DD[.⦘]]]")).join('\r\n')
%>

Can anyone tell me how to make this as days,hours format?
My current one returns only days.

Month ends in: <%+* let edate = moment(“2022-09-30”, “yyyy-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

Does anyone know how I would set it up to give me a date for the end of the year in 3 years?
so 2025-12-31?

How would I get a date that happens every 28 days? In other words, I have something I have to do every 28 days. Ideally, the output would be MM-DD-YYYY. I want to incorporate it into my daily notes.

I’d use the Tasks plugin

2 Likes

Thank you, no clue, why I hadn’t thought of that since I use it regularly. I was overthinking.