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:
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.
<%*
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.
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!
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.