Create links to a date array using Templater

What I’m trying to do

I am trying to create a listing of links to daily notes that fall within a specified range. I had previously found the following for links to all days within a given month:

<%* const currentMonthDates = new Array(
(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 %>

This works great for that purpose, but what I would like to do now is have a template that creates a listing of dates within and including dates I specify in frontmatter. For example:

This frontmatter:

start: 2023-07-05
end: 2023-07-08

Would yield:

  • [[2023-07-05]]
  • [[2023-07-06]]
  • [[2023-07-07]]
  • [[2023-07-08]]

Things I have tried

I have searched high and low through the forums, but haven’t found anything for this specific issue. I have also tried tinkering with the template that lists days in a month, but my programming ability is near non-existent.

Any help would be greatly appreciated.

Here, try this code:

const start = new moment(tp.frontmatter["start"]);
const end = new moment(tp.frontmatter["end"]);
const days = (end - start) / (1000 * 60 * 60 * 24);

for (let thisDay = 0; thisDay < days; thisDay++) {
	const thisDate = start.add(thisDay, 'days'); %>
- [[<% thisDate.format('YYYY-MM-DD') %>]]<%* } %>

Your code was trying to create an array of all the links in memory and then spitting it all out at once, whereas I find it’s usually easier to troubleshoot my template code if I keep it as simple as possible. In this case, I’m looping through the dates in your range and just rendering each date individually.

It could be worth while to check of those generated links actually exists or not, but then again that would depend on the use case.

But I do believe this approach to be a little nicer to the device creating the date range.

Thank you so much for the help with this. I’m not completely sure why, but when I used your code as written, it gave me the following result:

  • [[2023-07-05]]
  • [[2023-07-06]]
  • [[2023-07-08]]
  • [[2023-07-11]]

So it was essentially adding 1, then 2, then 3, and so on. Not sure why that was, but by changing the next to last line to

const thisDate = start.add(1, 'days'); %>

it worked the way I wanted it to. Because of this change, I also had to adjust the starting point to one day earlier). Your logic seems right, so I’m not sure why it worked the way it did.

Regardless, I got to the result I was looking for so thank you!

Ha ha! Yes, that’s definitely a bug! Good catch!

That’s what happens when I try to write code off the top of my head without actually testing it for you.

It’s not so much a bug as a feature on how moment changes the date you’re working on, and not creating a new date each time.

So at the end of this script your start had changed so its value is equal that of end. Not a problem in this context, but something to be aware of in other contexts.

Yeah, it’s a bug if it doesn’t pass the test. :slightly_smiling_face:

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