tp.date.weekday()
that’s a new one !
And I also had a lot of fun with Templater
, Periodic Notes
, Calendar
, some JS
but mostly Moment.js
(once someone told me on discord this could be done) yesterday while setting up my Daily/Weekly/Monthly and Yearly Notes .
I use mostly the tp.file.title
variable so most of what I use Templater for (for now) relies on how I format my daily (weekly/monthly/yearly) notes .
The tp.file.title
variable is very useful when using Calendar
/Periodic Notes
as it still allows me to create any daily notes (either in the past or future using Calendar
) without having to fear potential mistakes/errors.
As the non-dev’ person that I am , who has no
JS
knowledge (last time I did a little bit of javascript was 20 years ago ) avoiding mistakes/errors is quite important
.
But here is what I’ve got so far :
For the total of days (as a number) in a month :
In a Daily Note with the title formatted as YYYY-MM-DD
and using Moment.js
.daysInMonth() :
<% moment(tp.file.title,'YYYY-MM-DD').daysInMonth() %>
Example : for the daily note 2021-04-14
, the result will be 30
For the Start/End of a month :
In a Monthly Note with the title formatted as YYYY-MM
using Moment.js
.startOf() or .endOf() and the result formatted as ddd. DD/MM
<% moment(tp.file.title,'YYYY-MM').startOf('month').format('ddd. DD/MM') %>
Example : for the monthly note 2021-04
, the result will be Thu. 01/04
<% moment(tp.file.title,'YYYY-MM').endOf('month').format('ddd. DD/MM') %>
Example : for the monthly note 2021-04
, the result will be Fri. 30/04
For the number of days left in a month (using a daily notes as a starting point):
Daily Notes title formatted as YYYY-MM-DD
using .endOf() and .diff()
<% moment(moment(tp.file.title,'YYYY-MM-DD').endOf('month')).diff(moment(tp.file.title,'YYYY-MM-DD'),'days') %>
This is literally just a subtraction
.
In other words what it does is find the last day in the month from the date in the title of the daily notes (moment(tp.file.title,'YYYY-MM-DD').endOf('month')
) as its own Moment()
and then subtracts the days
from the daily notes title “date” (.diff(moment(tp.file.title,'YYYY-MM-DD'),'days'
)
So if you have a daily note titled 2021-04-14
the result will be 16
(as there are 30 days
in April
)
For getting a list of all days in a month (“pre-linked” if the daily note already exists):
In a Monthly Note with the title formatted as YYYY-MM
It took me a while to get this one, lots of trials and errors (first shared on Discord and its based on this Stack Overflow reply
)
<%* const currentMonthDates = new Array(
moment(tp.file.title,'YYYY-MM').daysInMonth()).fill(null).map(
(x, i) => moment(tp.file.title,'YYYY-MM').startOf('month').add(i,'days').format("- [[[]YYYY-MM-DD[#]YYYY-MM-DD[|]DD/MM [⦗]ddd[.⦘]]]")).join('\r\n') %>
<%* tR += currentMonthDates %>
What this kind of does is create a “list” (array) from the start of the month and as long as there are days in the month (based on the note’s title). Then it gives each item in this list a specific format (.format("- [[[]YYYY-MM-DD[#]YYYY-MM-DD[|]DD/MM [⦗]ddd[.⦘]]]")
).
In my case, once Templater
does what it has to do : - [[YYYY-MM-DD#YYYY-MM-DD|DD/MM ⦗ddd.⦘]]
.
There are lots of brackets []
because this is how Moment.js
escapes characters so it can correctly interpret the format I’m asking for .
Once the list is created and formatted, it gets rid of the comma ,
which originally separates each item in the list and replace it with a new line to get an actual unordered list .
The last part <%* tR += currentMonthDates %>
is to display the constant
created in the first part (following Templater Documentation) .
For the list of all months in a year (“pre-linked” if the monthly notes already exists):
In a Yearly Note with the title formatted as YYYY
.
Source : this Stack Overflow reply .
<% Array.apply(0, Array(12)).map(function(_,i){return moment(tp.file.title,'YYYY').month(i).format("- [[[]YYYY-MM[#]YYYY-MM[|]MM - MMMM[]]]")}).join('\r\n') %>
It “kind of” works in the same way as the previous one : creates a list of the months in the year referenced by the title, apply a specific format and then get rid of the comma ,
in the list .
Note :
- This part
Array(12)
is the length of the list (12 items) - This part
moment(tp.file.title,'YYYY')
is the starting point of the list (based on the note’s title)
To get the DateTime (Now()
) in whatever language you want :
<% moment(tp.date.now("YYYY-MM-DD HH:MM")).locale('fr').format("LLLL") %>
Updated and simplified in the post below
:
… I might find others .
I’m absolutely no JS
expert (far from that ) so there might be better ways to get to the same results too
!
Edit : Added some context, explanations (as best as I could , trying to make this post more understandable for non-dev’ people like myself
) and sources
.