Skip to content

Instantly share code, notes, and snippets.

@dannberg
Last active March 23, 2024 12:54
Show Gist options
  • Save dannberg/48ea2ba3fc0abdf3f219c6ad8bc78eb6 to your computer and use it in GitHub Desktop.
Save dannberg/48ea2ba3fc0abdf3f219c6ad8bc78eb6 to your computer and use it in GitHub Desktop.
Dann Berg's Daily Note Template for Obsidian. Uses Dataview & Templater plugins. Should be saved as a Markdown file in Obsidian. Read the full tour: https://dannb.org/blog/2022/obsidian-daily-note-template/
---
created: <% tp.file.creation_date() %>
---
tags:: [[+Daily Notes]]
# <% moment(tp.file.title,'YYYY-MM-DD').format("dddd, MMMM DD, YYYY") %>
<< [[Timestamps/<% tp.date.now("YYYY", -1) %>/<% tp.date.now("MM-MMMM", -1) %>/<% tp.date.now("YYYY-MM-DD-dddd", -1) %>|Yesterday]] | [[Timestamps/<% tp.date.now("YYYY", 1) %>/<% tp.date.now("MM-MMMM", 1) %>/<% tp.date.now("YYYY-MM-DD-dddd", 1) %>|Tomorrow]] >>
---
### 📅 Daily Questions
##### 🌜 Last night, after work, I...
-
##### 🙌 One thing I'm excited about right now is...
-
##### 🚀 One+ thing I plan to accomplish today is...
- [ ]
##### 👎 One thing I'm struggling with today is...
-
---
# 📝 Notes
- <% tp.file.cursor() %>
---
### Notes created today
```dataview
List FROM "" WHERE file.cday = date("<%tp.date.now("YYYY-MM-DD")%>") SORT file.ctime asc
```
### Notes last touched today
```dataview
List FROM "" WHERE file.mday = date("<%tp.date.now("YYYY-MM-DD")%>") SORT file.mtime asc
```
@mapoulos
Copy link

mapoulos commented Sep 6, 2022

Note the change to single quotes after tp.date.now( and before )%> here:

List FROM "" WHERE file.cday = date("<%tp.date.now('YYYY-MM-DD')%>") SORT file.ctime asc

and here

List FROM "" WHERE file.mday = date("<%tp.date.now('YYYY-MM-DD')%>") SORT file.mtime asc

@dannberg
Copy link
Author

dannberg commented Sep 7, 2022

Thanks for sharing! I had to fix the quotation marks on ll. 31 and 36 to get this to work, and enable the "Trigger Templater on new file creation" option.

I just confirmed in my Obsidian that it's working just perfectly for me with double-quotes rather than single-quotes around YYY-MM-DD. I wonder where the difference is that's causing it to break for you but work for me.

@moritzdawo
Copy link

I just confirmed in my Obsidian that it's working just perfectly for me with double-quotes rather than single-quotes around YYY-MM-DD. I wonder where the difference is that's causing it to break for you but work for me.

For me, it worked fine too. Only the template showed an Dataview Error, that you can fix with single quotes.

Oh and I added an <% tp.file.cursor() %> to the Notes Section, so you can start writing, when Obsidian starts with your daily note.

Thank you very much for this very great template!

@dannberg
Copy link
Author

Oh and I added an <% tp.file.cursor() %> to the Notes Section, so you can start writing, when Obsidian starts with your daily note.

That's a cool trick! I didn't even know that existed. I'm going to update the gist, and start using that myself, too.

@LovisR
Copy link

LovisR commented Sep 24, 2022

sry if thats a supid question, but does +Daily Notes do anything? or is it just the name of the file that you link all your daily notes to?

@Aron-Polner
Copy link

Thanks for sharing!

To make this work you have to enable some options in Templater.

I enabled the following:

  • Automatic jump to cursor
  • Trigger Templater on new file creation

@mt190502
Copy link

Hello. I'm trying to build my personal obsidian vault. While doing research, I came across this page and ran into a problem. With tp I get the value of the previous and next day. But when I click on it, I encounter the problem in the picture below.

image

image

@dannberg
Copy link
Author

dannberg commented Oct 11, 2022

Hello. I'm trying to build my personal obsidian vault. While doing research, I came across this page and ran into a problem. With tp I get the value of the previous and next day. But when I click on it, I encounter the problem in the picture below.

@mt190502 It looks like the Templater plugin is not firing when the new page is created. Templater should turn the <% %> tags into text, and it looks like that's not happening here. I'd review your settings in the Templater Plugin settings screen? Not 100% sure where the error is, but that's where I'd check first.

@mt190502
Copy link

Hello. I'm trying to build my personal obsidian vault. While doing research, I came across this page and ran into a problem. With tp I get the value of the previous and next day. But when I click on it, I encounter the problem in the picture below.

It looks like the Templater plugin is not firing when the new page is created. Templater should turn the <% %> tags into text, and it looks like that's not happening here. I'd review your settings in the Templater Plugin settings screen? Not 100% sure where the error is, but that's where I'd check first.

Thank you very much for your interest. After hours of research, I solved the problem <3

@dannberg
Copy link
Author

Thank you very much for your interest. After hours of research, I solved the problem <3

Do you remember how you solved it? It might be useful to share here in case future people have the same issue.

@mt190502
Copy link

Thank you very much for your interest. After hours of research, I solved the problem <3

Do you remember how you solved it? It might be useful to share here in case future people have the same issue.

Of course. First of all, I created the "_templates" folder in the main directory and created my templates. I selected the folder first in Settings > Templater. Then I opened the 'New File Creation Trigger' option and connected the templates to the folders as in the photo below. I ran into a few syntax errors. It worked when I fixed them too

image

@dannberg
Copy link
Author

Just updated the Yesterday/Tomorrow links to use Templater's moment() function. The old code would have broke if creating daily notes for the future.

From Lopyter on Reddit:

As they are right now, the links will be broken if you ever decide to create a day's notes before the actual date. If you want to plan the day for tomorrow, the "yesterday" link in the note will link to today's yesterday (so 2 days ago from the note's POV) and the "tomorrow" link will link to the actual note itself. Not very useful and a potential point of confusion/frustration down the line.

@123m321
Copy link

123m321 commented Oct 26, 2022

I noticed it didn't work when using another Date Format in the settings of Daily Notes (I used DD MMM, had to remove it)

@dannberg
Copy link
Author

I noticed it didn't work when using another Date Format in the settings of Daily Notes (I used DD MMM, had to remove it)

Good call. You should be able to update the Templater code to mirror the Date Format you prefer. I should add a disclaimer to the blog post. Thanks.

@Rot4n
Copy link

Rot4n commented Nov 4, 2022

For daily notes that are created in the past or future it would be better to determine the date from filename instead of current date. Here's how I did it:

For created notes:

list from "" where file.cday = date("<% moment(tp.file.title,"YYYY.MM.DD").format("YYYY-MM-DD") %>") sort file.ctime asc

And for modified notes (excluding created notes of the same day as suggested by @Steellow ):

list from ""
where
  file.mday = date("<% moment(tp.file.title,"YYYY.MM.DD").format("YYYY-MM-DD") %>")
  and 
  file.cday != date("<% moment(tp.file.title,"YYYY.MM.DD").format("YYYY-MM-DD") %>")
sort file.mtime asc

The moment(tp.file.title,"YYYY.MM.DD") part indicates that moment() should read the file title in "YYYY.MM.DD" format, which is the one I use, so adjust accordingly.
Could be useful for tomorrow/yesterday links as well.

I hope it might be useful to someone.

@Aron-Polner
Copy link

Has anyone solved the "issue" when you try to jump from a Monday to a Friday?
Is there a smart way one could script so that it finds the closest existing note?

@ayyazdaniaryan
Copy link

@Aron-Polner oof, don't take my word for it, but you could create a user functions like previousDate() and nextDate(), pull in moment-business-days using unpkg in your custom defined functions, and then add them to the tp.date object (this object IS the moment object source).

This should enable you to use them in your template accordingly to get that wrap around. I suspect it'll slow down templater launch on first go, however. YMMV

@chump
Copy link

chump commented Dec 17, 2022

Not a real coder here - I've got

  • the templates enabled
  • the settings switched on I believe
  • and a new empty daily note in the templates folder as the instructions say

but where exactly do I paste the raw template code here?

@chump
Copy link

chump commented Dec 17, 2022

I'm guessing I just paste it into the empty template note in source mode, but for me

<% moment(tp.file.title,'YYYY-MM-DD').format("dddd, MMMM DD, YYYY") %>

still shows up coded rather than the date

and I get this in the bottom sections

Notes created today
Dataview: Error:
-- PARSING FAILED --------------------------------------------------

1 | List FROM "" WHERE file.cday = date("<%tp.date.now('YYYY-MM-DD")%>") SORT file.ctime asc
| ^
2 |

Expected one of the following:

'(', 'null', boolean, date, duration, file link, list ('[1, 2, 3]'), negated field, number, object ('{ a: 1, b: 2 }'), string, variable
Dataview: Error:
-- PARSING FAILED --------------------------------------------------

1 | list from "" where file.cday = date("<% moment(tp.file.title,"YYYY.MM.DD").format("YYYY-MM-DD") %>") sort file.ctime asc
| ^
2 |

Expected one of the following:

'(', ')', '*' or '/' or '%', '+' or '-', ',', '.', '>=' or '<=' or '!=' or '=' or '>' or '<', '[', 'and' or 'or'
Notes last touched today
Dataview: Error:
-- PARSING FAILED --------------------------------------------------

1 | List FROM "" WHERE file.mday = date("<%tp.date.now('YYYY-MM-DD")%>") SORT file.mtime asc
| ^
2 |

Expected one of the following:

'(', 'null', boolean, date, duration, file link, list ('[1, 2, 3]'), negated field, number, object ('{ a: 1, b: 2 }'), string, variable

@jiwonsim
Copy link

I also had same problems as @chump 's.

Dataview: Error: 
-- PARSING FAILED --------------------------------------------------

  1 | 
> 2 | List FROM "" WHERE file.cday = date("<% tp.date.now("YYYY-MM-DD") %>") SORT file.ctime asc
    |                                                      ^
  3 | 

Expected one of the following: 

'(', ')', '*' or '/' or '%', '+' or '-', ',', '.', '>=' or '<=' or '!=' or '=' or '>' or '<', '[', 'and' or 'or'

@dannberg
Copy link
Author

@jiwonsim @chump I see a "parsing failed" error when viewing the Daily Note Template post itself. When you create a new note using that template, with the Templater plugin working correctly, it should parse the code correctly in the new note that was created from the template.

@chump
Copy link

chump commented Dec 21, 2022

I figured out the issue with dataview - when i was pasting the code, for some reason it was creating some extra spaces. However @dannberg I'm still for some reason having issues with this part of the code

# <% moment(tp.file.title,'YYYY-MM-DD').format("dddd, MMMM DD, YYYY") %>
It's not just yours though, other templates where moment used weren't working for me, although the yesterday/tomorrow part works oddly.

@Claustn
Copy link

Claustn commented Dec 22, 2022

This is the first time I am playing with this, but I think if you add a "+" after the % like this:

# <%+ moment(tp.file.title,'YYYY-MM-DD').format("dddd, MMMM DD, YYYY") %>

@chump
Copy link

chump commented Dec 22, 2022

Wow!! That did it. I'm confused as to why that works? I don't see the + in anyone's sample codes. major thanks for solving that one for me @Claustn I've been racking my brain for days on it.

@dannberg
Copy link
Author

That's so strange. I have no idea what the + does there, but I'm glad you got it working!

If anyone reading this knows why some people need a + and others don't, let me know and I can update the post.

@Claustn
Copy link

Claustn commented Dec 22, 2022

@Nexact
Copy link

Nexact commented Dec 31, 2022

I was missing the tasks that I've not completed from other days and modified the template a bit! If you think it'd be useful...

##### 🚀 Thing(s) from other days that I need to finish
```
dataviewjs
 dv.taskList(dv.pages('"Timestamps"').file.tasks.where(t => !t.completed && t.text != ""));
```

@7200rpm
Copy link

7200rpm commented Jan 5, 2023

Just noticing that the section for "Notes created today" uses the current date, so if you create a note for a day in the future or past, it won't give the correct info. I sometimes create future or past notes for upcoming info or if I missed a day. Is there an easy way to update this?

@szhydkov
Copy link

szhydkov commented Jan 16, 2023

I am just staring out with Obsidian and came across this great template. However, I also followed the suggestion of storing daily notes in "Timestamps/Year/Month/Day", in which case the Yesterday and Tomorrow buttons don't quite work (they create a file in the root directory and without the template. I solved it by updating the code for the tomorrow button as follows:

fileDate = moment(tp.file.title, 'YYYY-MM-DD-dddd').add(1, 'd').format("[Timestamps]/YYYY/MM-MMMM/YYYY-MM-DD-dddd")

and similarly for the yesterday button. Then in the Templater settings, under "Folder Templates", I added the "Timestamps" folder with the daily note template (wherever it is stored for you). Now, when creating new notes with the yesterday and tomorrow buttons, it seems to work, hopefully this will help someone with the same problem.

I will now look for easy ways to create a note for an arbitrary day but at worst you can just create a new note with the date in the title.

@anatexis
Copy link

@szhydkov thanks! I had the same issue and that solved it perfectly!

@amidstwoods
Copy link

I am just staring out with Obsidian and came across this great template. However, I also followed the suggestion of storing daily notes in "Templates/Year/Month/Day", in which case the Yesterday and Tomorrow buttons don't quite work (they create a file in the root directory and without the template. I solved it by updating the code for the tomorrow button as follows:

fileDate = moment(tp.file.title, 'YYYY-MM-DD-dddd').add(1, 'd').format("[Timestamps]/YYYY/MM-MMMM/YYYY-MM-DD-dddd")

and similarly for the yesterday button. Then in the Templater settings, under "Folder Templates", I added the "Timestamps" folder with the daily note template (wherever it is stored for you). Now, when creating new notes with the yesterday and tomorrow buttons, it seems to work, hopefully this will help someone with the same problem.

I will now look for easy ways to create a note for an arbitrary day but at worst you can just create a new note with the date in the title.

I had the same problem with yesterday and tomorrow dates that don't yet exist and this solved it, thanks so much!

@timnashcouk
Copy link

timnashcouk commented Jan 22, 2023

For folks who have a daily note with a non date name, or in a non US format, rather then using:
# <% moment(tp.file.title,'YYYY-MM-DD').format("dddd, MMMM DD, YYYY") %>
Which uses the file title for date formatting you can use:
moment(tp.file.creation_date(),'YYYY-MM-DD').format("dddd, MMMM DD, YYYY") %>
This allows you to name your Daily Notes in anyway you see fit (In my case I use a different date format with some extra characters), instead it takes the date the file created instead.

Which means now my daily notes are not from # Wednesday, January 20, 2021 😆

@prime-optimal
Copy link

prime-optimal commented Feb 1, 2023

and similarly for the yesterday button. Then in the Templater settings, under "Folder Templates", I added the "Timestamps" folder with the daily note template (wherever it is stored for you). Now, when creating new notes with the yesterday and tomorrow buttons, it seems to work, hopefully this will help someone with the same problem.

This is what worked for me. I had been storing my Daily Notes in the "Daily Notes" folder and didn't realize that the [Timestamps] part of the code actually meant the Timestamps folder. Now it works and I think this is a much better daily note template than what I was using before.

Thank you and good job OP and everyone else who helped, this was a real team effort lol

@djchateau
Copy link

tags:: [[+Daily Notes]]

I noticed all your templates have tags set with tags:: instead of tags: and I've searched through the Obsidian reference material could not understand why you add that extra colon when it's not part of the syntax. Any chance you could explain what's going on here?

@dannberg
Copy link
Author

@djchateau ha, great eyes. The short answer is that the extra colon does absolutely nothing. You can ignore.

The longer answer is that this is a holdover from when I was using Roam Research. On that platform, using two colons to denote tags gave it specific functionality. I never really fully wrapped my head around Roam Research, or using block-based note-taking to the fullest, but the double colon is a habit that came over with me when I moved to Obsidian. It has NO functionality in Obsidian, so I never really spent time to 1) break the muscle memory or 2) had motivation to find & replace in all my imported notes. Therefore, tags:: is just located everywhere in my Obsidian system, in both old notes and new.

@guentur
Copy link

guentur commented Feb 22, 2023

@dannberg

@jiwonsim @chump I see a "parsing failed" error when viewing the Daily Note Template post itself. When you create a new note using that template, with the Templater plugin working correctly, it should parse the code correctly in the new note that was created from the template.

Hi. This parsing error appears because of the double quotes inside outer double quotes. Maybe it will be understandable directly in my fix:
image

Just change the double quotes to single quotes:

### Notes created today

List FROM "" 
WHERE file.cday = date("<%tp.date.now('YYYY-MM-DD')%>") 
SORT file.ctime asc

### Notes last touched today

List FROM "" WHERE file.mday = date("<%tp.date.now('YYYY-MM-DD')%>") SORT file.mtime asc

It works fine for me when I employ the template

Thanks for your blog post. It really made my day better.

@xBlady
Copy link

xBlady commented Mar 2, 2023

THanks to the author for grat work, I have a question, how could I make sure new notes are created wthin timestamps folder using the same structure as creating new daily note, when creating tommorows or yeserday's note by below function? Right now it creates new files in main folder.

<< [[<% fileDate = moment(tp.file.title, 'YYYY-MM-DD-dddd').subtract(1, 'd').format('YYYY-MM-DD-dddd') %>|Yesterday]] | [[<% fileDate = moment(tp.file.title, 'YYYY-MM-DD-dddd').add(1, 'd').format('YYYY-MM-DD-dddd') %>|Tomorrow]] >>

@szhydkov
Copy link

szhydkov commented Mar 2, 2023

THanks to the author for grat work, I have a question, how could I make sure new notes are created wthin timestamps folder using the same structure as creating new daily note, when creating tommorows or yeserday's note by below function? Right now it creates new files in main folder.

<< [[<% fileDate = moment(tp.file.title, 'YYYY-MM-DD-dddd').subtract(1, 'd').format('YYYY-MM-DD-dddd') %>|Yesterday]] | [[<% fileDate = moment(tp.file.title, 'YYYY-MM-DD-dddd').add(1, 'd').format('YYYY-MM-DD-dddd') %>|Tomorrow]] >>

See my comment above: https://gist.github.com/dannberg/48ea2ba3fc0abdf3f219c6ad8bc78eb6?permalink_comment_id=4438780#gistcomment-4438780

@Razzokk
Copy link

Razzokk commented Mar 22, 2023

I wanted to share my solution to this. I am not the most consequent daily note taking guy out there and thus there may be days where I don't take notes. The provided solution doesn't work for me as it only looks at the day before and after.

With some help of the Dataview plugin we can retrieve the "latest previous" and "earliest next" notes. In the frontmatter block I additionally have the date and a tag daily-note. In the template the frontmatter block looks like this:

---
tag: daily-note
date: {{date}}
---

And this is the code that I use for the previous/next daily note navigation:

```dataviewjs
const DAILY_NOTES = dv.pages('"<path to daily notes directory>" and #daily-note');
const TODAY_DATE = dv.current().date;

const PREV_NOTE = DAILY_NOTES
	.where(note => note.date < TODAY_DATE)
	.sort(note => note.date, 'desc')
	.limit(1)[0];

const NEXT_NOTE = DAILY_NOTES
	.where(note => note.date > TODAY_DATE)
	.sort(note => note.date, 'asc')
	.limit(1)[0];

const PREV = PREV_NOTE ? `[[${PREV_NOTE.file.name}|Previous]]` : 'None';
const NEXT = NEXT_NOTE ? `[[${NEXT_NOTE.file.name}|Next]]` : 'None';

dv.paragraph(`<< ${PREV} | ${NEXT} >>`);
```

@mcgridles
Copy link

For the "previous/next" issue, I came across this similar blog post about someone else's Obsidian daily notes template which links to this DataviewJS snippet which seems pretty airtight FWIW.

@dannberg
Copy link
Author

Thanks all. I've added a link to that blog post in the original post.

@nnnolan
Copy link

nnnolan commented Jun 21, 2023

i love you @dannberg thank you for being the gateway drug to a new micro obsessive productivity increasing tool

@dannberg
Copy link
Author

@nnnolan happy to be an enabler

@tommdq
Copy link

tommdq commented Jul 22, 2023

I got this for the date
# <%+ moment(tp.file.title,'YYYY-MM-DD').format("dddd, MMMM DD, YYYY") %>

But when i'm getting "Invalid Date" : (

@Err0rNull
Copy link

I suspect this is because you are trying to look at your Template-version of the file in readview, but it'll only work when used in an execution of a new file made using the template.

@SebastianBitsch
Copy link

For anyone not using the Templater plugin for anything else than getting the date, i found that you can just rely on the core plugin to get the current date with {{date}}. i.e:

List FROM "" WHERE file.cday = date({{date}}) SORT file.ctime asc

and

List FROM "" WHERE file.mday = date({{date}}) SORT file.mtime asc

It will throw an error in the template itself, but work just fine in a note made from the template

@McAldo
Copy link

McAldo commented Nov 6, 2023

Interesting thanks!

Out of curiosity, why not using simply

Last modified today

List FROM "" WHERE file.mday = this.file.day AND file.cday != this.file.day SORT file.mtime asc

I tend and use 'this.file.day', or similar, if it is withing a dataview query anyway.
I can see an advantage in hard coding the date though, if that section needed to be copied somewhere else though.

What I would really love would be some solution to hard code the full ouput there.
As soon as a file is modified again, it will disapper from the list in that specific day, making the dataview item less useful to track what we have worked on that specific day.

@dannberg
Copy link
Author

dannberg commented Nov 7, 2023

@McAldo The original thought was to hardcode the date because on this specific note, I'll always want files that were last modified on that specific date. So I might as well hardcode it.

I'm not familiar with this.file.day. Does it lock to the date the file was created? Or does it change when the daily note is modified? If it's the former, than it could definitely be a replacement for the hardcoded date in my template. However, I'll sometimes edit past days' Daily Notes, in which case I want to make sure this list still only shows notes that were last touched on the specific date of that Daily Note.

@McAldo
Copy link

McAldo commented Nov 7, 2023

@dannberg As I understand, this.file.day queries the creation date of the note containing the code, so effectively it achieves the same result without hardcoding, I find it makes for more readable code and will work independently from templater parsing . So, for instance, I use it to quickly add the same code to past notes, if I come up with a new idea for a query and I would like old notes to have that too.
However, your approach has some advantages, for instance one could copy and paste the resulting output into another note created on different day, and the ouput will be the same, still referring to the original date. With my code doing so would have it query whatever creation date the new note has.

In reality, the most useful thing for me would be to have a list of all edited notes on that day, which stays static. So if on Monday and thursday I worked on note X, both daily notes will show X, rather than only the thursday one.
It can be done, with dataview js and dv.markdownList(values) or dv.markdownTable(headers, values), which hardcode the whole ouput.
But it is not a great solution becasue it would need to be triggered every day after the last note of the day was modified, once created it will not update.

There is a plugin (list last edited, I think it is called) which is supposed to do that, but does not work well for me.

@geek-andi
Copy link

This is the first time I am playing with this, but I think if you add a "+" after the % like this:

# <%+ moment(tp.file.title,'YYYY-MM-DD').format("dddd, MMMM DD, YYYY") %>

To resolve the syntax on note creation, you need to enable the Templater setting "Trigger Templater on new file creation". IMHO no need for a dynamic command there, as linked later: https://github.com/SilentVoid13/Templater/blob/master/docs/src/commands/dynamic-command.md

@dannberg This would be a usefull info in your blog post (or as comment in the template, if that's possible)

@dannberg
Copy link
Author

@geek-andi Good call. Just added this additional information to the Setting up your Daily Note template section at the bottom of the blog post. Also linked back to this comment section in case anyone wants to read more.

@AsheeshSrivastava
Copy link

Just noticing that the section for "Notes created today" uses the current date, so if you create a note for a day in the future or past, it won't give the correct info. I sometimes create future or past notes for upcoming info or if I missed a day. Is there an easy way to update this?

If you use the date as the title for the Daily Note, you can use dataviewjs to get the date from the title and then use it for code forNotes created today. I am sharing my code. my Daily note title is in Do MMM Y format so you need to customise for your own format.

function parseCustomDate(dateStr) {
    const dateRegex = /(\d+)(st|nd|rd|th) (\w+) (\d{4})/;
    const match = dateStr.match(dateRegex);
    if (!match) return null;

    const [ , dayPart, , month, year] = match;
    const day = parseInt(dayPart.match(/\d+/)[0], 10); // Extract just the numeric part of the day
    const months = {
        "Jan": 0, "Feb": 1, "Mar": 2, "Apr": 3, "May": 4, "Jun": 5,
        "Jul": 6, "Aug": 7, "Sep": 8, "Oct": 9, "Nov": 10, "Dec": 11
    };
    const monthIndex = months[month];
    return new Date(year, monthIndex, day);
}

function formatDateISO(date) {
    const pad = (number) => number.toString().padStart(2, '0');
    return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
}

function ensureArray(value) {
    if (Array.isArray(value)) return value;
    return value ? [value] : [];
}

const currentNoteTitle = dv.current().file.name;
const targetDate = parseCustomDate(currentNoteTitle);

if (targetDate) {
    const formattedTargetDate = formatDateISO(targetDate);
    const pages = dv.pages()
        .where(p => p.file.ctime || p.file.mtime)
        .filter(p => {
            const creationDate = formatDateISO(new Date(p.file.ctime));
            const modifiedDate = formatDateISO(new Date(p.file.mtime));
            return creationDate === formattedTargetDate || modifiedDate === formattedTargetDate;
        });

    if (pages.length) {
        // Start the table.
        dv.table(["File", "Folder", "Tags", "Status"],
            pages.map(page => {
                return [
                    // Link to the file.
                    `[[${page.file.path}]]`,
                    // Folder of the file.
                    page.file.folder,
                    // Tags of the file, making sure to handle the case where tags may not be an array.
                    ensureArray(page.tags).join(", "),
                    // Status of the file.
                    page.status || ''
                ];
            })
        );
    } else {
        dv.paragraph(`No notes were found created or modified on ${formattedTargetDate}.`);
    }
} else {
    dv.paragraph("The current note's title does not contain a valid date.");
}

@adall
Copy link

adall commented Dec 8, 2023

Thanks for sharing this @dannberg - much appreciated. I'm enjoying experimenting with it. Heads up there could be a typo in one of the Daily Questions headings. I changed one of them from:

One thing I've excited about right now is...

To this:

One thing I'm excited about right now is...

@dannberg
Copy link
Author

dannberg commented Dec 8, 2023

@adall ah, good catch. I fixed that a while back in my personal library, but hadn't corrected the gist. Just made the update. Thanks!

@Robiton
Copy link

Robiton commented Jan 14, 2024

I have two concerns, figuring I am missing something on the one.

image

When I click yesterday, it makes a new note, but in a folder up. It also does not apply the template other than the name.
image

Clicking new daily note does work for today of course.

@faying93
Copy link

For folks who have a daily note with a non date name, or in a non US format, rather then using: # <% moment(tp.file.title,'YYYY-MM-DD').format("dddd, MMMM DD, YYYY") %> Which uses the file title for date formatting you can use: moment(tp.file.creation_date(),'YYYY-MM-DD').format("dddd, MMMM DD, YYYY") %> This allows you to name your Daily Notes in anyway you see fit (In my case I use a different date format with some extra characters), instead it takes the date the file created instead.

Which means now my daily notes are not from # Wednesday, January 20, 2021 😆

Hey, I use the format you meantioned which is instead of file title I use the 'file creation date' , but when I click on 'tomorrow' or 'yesterday, I am navigated to a page titled invalid date. Can you help? I understand I need to make changes in the template formula, but I am not sure how. Thank you

@gd03champ
Copy link

gd03champ commented Jan 28, 2024

Heya people 👋

I've made some changes in the template that solves most of the problem and so just sharing here

https://gist.github.com/gd03champ/6c8fe7739161c731859298b1bc96cfa0

  • Notes created and Notes last touched always shows todays data weather you are on today's note or some other day's note
  • Yesterday and tomorrow backlink doesn't create new note in organized sub directory

Have a great day !

@Robiton
Copy link

Robiton commented Feb 21, 2024

Figured out the issue with the template not applying to new notes. Though now I get a second note if I use the yesterday button.

Here is the code I am using. Just not seeing the issue.


created: <% tp.file.creation_date() %>

tags:: [[+Daily Notes]]

<% moment(tp.file.title,'YYYY-MM-DD').format("dddd, MMMM DD, YYYY") %>

<< [[Daily Notes/<% fileDate = moment(tp.file.title, 'YYYY-MM-DD-dddd').subtract(1, 'd').format("/YYYY/MM-MMMM/YYYY-MM-DD-dddd") %>|Yesterday]] | [[Daily Notes/<% fileDate = moment(tp.file.title, 'YYYY-MM-DD-dddd').add(1, 'd').format("/YYYY/MM-MMMM/YYYY-MM-DD-dddd") %>|Tomorrow]] >>

I did have to add this to templater,
image

Thoughts?
Thanks in advance

@Gryn23
Copy link

Gryn23 commented Feb 28, 2024

For everyone who has trouble because clicking the yesterday and tomorrow links will render empty files with just a title:
There's a small omission in the blog post. You need to go to settings > Templater > scroll down to Folder templates.

  1. Check "Enable folder templates" on
  2. Add New:
    Here you set the folder to "Timestamps" and Template to "Extra/Templates/Template, Daily Note.md"

Done! Now when you click tomorrow or yesterday, a new note will be made if it isn't there already, containing your template.

(haha, I'm a bit proud of myself for finding out, as this is the third day I use any PKM app in my life, and the first day I discovered templates!)

PS: also please use @szhydkov's useful correction of the folder structure above, so your new files are put in the right folder automatically.
Thanks for the script @dannberg. Could you please update it with the above?

@Robiton
Copy link

Robiton commented Feb 28, 2024

For everyone who has trouble because clicking the yesterday and tomorrow links will render empty files with just a title: There's a small omission in the blog post. You need to go to settings > Templater > scroll down to Folder templates.

1. Check "Enable folder templates" on

2. Add New:
   Here you set the folder to "Timestamps" and Template to "Extra/Templates/Template, Daily Note.md"

Done! Now when you click tomorrow or yesterday, a new note will be made if it isn't there already, containing your template.

(haha, I'm a bit proud of myself for finding out, as this is the third day I use any PKM app in my life, and the first day I discovered templates!)

PS: also please use @szhydkov's useful correction of the folder structure above, so your new files are put in the right folder automatically. Thanks for the script @dannberg. Could you please update it with the above?

When you do this, if you are on today and click to go back to yesterday, does it make a duplicate with a number like this?
image

That is the issue I noticed after adding the templater setup

@Gryn23
Copy link

Gryn23 commented Feb 28, 2024

No, it doesn't. If the file already exists, it just goes to that file. If the file doesn't exist yet, it's created.

@Robiton
Copy link

Robiton commented Feb 28, 2024

No, it doesn't. If the file already exists, it just goes to that file. If the file doesn't exist yet, it's created.

Hrm, how does the code I have compare to yours?

@dannberg
Copy link
Author

dannberg commented Feb 29, 2024

@Robiton @Gryn23 I'm having the same problem at @Gryn23.

I updated the "tomorrow" link Templater code to [[Timestamps/<% fileDate = moment(tp.file.title, 'YYYY-MM-DD-dddd').add(1, 'd').format('YYYY-MM-DD-dddd') %>|Tomorrow]]. Then, I modified Templater settings so that all new files created in the Timestamps directory would have the Daily Note template applied automatically.

It works great when clicking that link to create a new note.

However, if a note currently exists (such as the Yesterday link), it does not recognize the note. Because the Timestamps/ in the [[]] link helps for creating a new note, but isn't necessary when linking to an existing note.

@Robiton if you have a solution for this, let me know. I was in the process of updating the original post but couldn't get it working myself.

@dannberg
Copy link
Author

@Robiton @Gryn23 Scratch that. I got it working. It doesn't really handle leap day correctly (of course I'm playing with it on 2/29 lol) but it should work every other day. I've updated the gist with the working code.

@Gryn23
Copy link

Gryn23 commented Mar 2, 2024

Nice!

Strange that leap day gave a problem with you, it didn't give problems with me.

This is my slightly altered version of your script, but I don't think there are big differences. Main difference is that I use it for daily to do lists, and I wanted the notes as a possibility, but out of the way to keep distractions at bay. Hence the many <br> lines. Also the date format is changed to Dutch order:

`tags: #task #todo

<% moment(tp.file.title, 'YYYY-MM-DD').format("dddd, DD MMMM, YYYY") %>

<< [[<% fileDate = moment(tp.file.title, 'YYYY-MM-DD-dddd').subtract(1, 'd').format('[To_do]/YYYY/MM-MMMM/YYYY-MM-DD-dddd') %>|Yesterday]] | [[<% fileDate = moment(tp.file.title, 'YYYY-MM-DD-dddd').add(1, 'd').format('[To_do]/YYYY/MM-MMMM/YYYY-MM-DD-dddd') %>|Tomorrow]] >>

---

#### To Do

- [ ] 

<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>

---
#### Notes

---
#### Notes created today

List FROM "" WHERE file.cday = date("<%tp.date.now("YYYY-MM-DD")%>") SORT file.ctime asc

#### Notes last touched today

List FROM "" WHERE file.mday = date("<%tp.date.now("YYYY-MM-DD")%>") SORT file.mtime asc

@Robiton
Copy link

Robiton commented Mar 2, 2024

@Robiton @Gryn23 Scratch that. I got it working. It doesn't really handle leap day correctly (of course I'm playing with it on 2/29 lol) but it should work every other day. I've updated the gist with the working code.

Sadly, updated code still creates a -1 if there is already a note for that day.

Anyone have any other ideas?

@dannberg
Copy link
Author

dannberg commented Mar 3, 2024

Hm, I'm at a loss.

The challenge is that the Daily Note template uses today (file.cday) as a base for all the Templater code. Which means if you click on "Tomorrow" in today's daily note to create tomorrow's note, then Tomorrow's note will have broken yesterday/tomorrow links as well as the two dataview tables.

I don't believe there's a way to pass through information through a link. Meaning, if you click on Tomorrow, you can tell the new note that you actually want file.cdate + 1 for all the Templater code.

Maybe there's a way to store the date we want as a variable in the note? Although I'm totally blanking on how to actually execute this.

@Gryn23
Copy link

Gryn23 commented Mar 6, 2024

@Robiton @dannberg I have no idea why it's not working with you guys, it is with me. If the note already exists, the tomorrow link will just open that file in my vault. Here's my latetst code. The code I posted before was already working, but since then I installed:

  1. Update time on edit
  2. Calender
  3. Periodic notes

explanation:

  1. plugin 1 makes sure that the date created and date modified are written into the properties of the file, and not overwritten when you migrate to a new computer, or sync to another device.
  2. plugin 2 speaks for itself. It will ask you if you want to migrate daily notes to it: do so.
  3. plugin 3 allows me to make a weekly note as well. You need to enter the settings and point the plugin to where your daily notes folder and your week folder lives, and where the templates for the two live.
  4. After that you need to do that again in Templater (!)
    afbeelding

This is the most important code of my daily note template:

`##### <% moment(tp.file.title, 'YYYY-MM-DD-DDDD').format("dddd, DD MMMM YYYY") %>

<< [[<% fileDate = moment(tp.file.title, 'YYYY-MM-DD-dddd').subtract(1, 'd').format('[To_do/Dag]/YYYY/MM-MMMM/YYYY-MM-DD-dddd') %>|Yesterday]] | [[<% fileDate = moment(tp.file.title, 'YYYY-MM-DD-dddd').add(1, 'd').format('[To_do/Dag]/YYYY/MM-MMMM/YYYY-MM-DD-dddd') %>|Tomorrow]] >>`

@Gryn23
Copy link

Gryn23 commented Mar 6, 2024

This is the template for my weekly note. Of course you need to change the names of the folder, and translate "Volgende week" with 'next week'.

`##### <% moment(tp.file.title, 'YYYY-[W]-WW').format("[Week] ww - DD MMMM YYYY") %>

<< [[<% fileDate = moment(tp.file.title, 'YYYY-[W]-WW').subtract(1, 'w').format('[To_do/Week]/YYYY/MM-MMMM/YYYY-[W]-WW') %>|Vorige week]] | [[<% fileDate = moment(tp.file.title, 'YYYY-[W]-WW').add(1, 'w').format('[To_do/Week]/YYYY/MM-MMMM/YYYY-[W]-WW') %>|Volgende week]] >>`

@Gryn23
Copy link

Gryn23 commented Mar 6, 2024

My properties are in the template too. I've hidden properties after I made sure they work. Unhidden this is what shows in the template:

afbeelding

And this is what's spit out if I make a weekly note, (but only if you do so in the right folder! Select the right folder, click right and choose new note, or click "volgende week" on an existing note. Making weeknumbers visible in Calender and clicking an empty weeknumber (or day) works as well.) As you see the "Update time on edit" plugin automatically added date properties.

afbeelding

@pantsmasterson
Copy link

Am I the only person for whom this code just stays as pasted-in code when I start a new daily note?

Notes
<% tp.file.cursor() %>

It just stays in the note, like so:

image

@Gryn23
Copy link

Gryn23 commented Mar 21, 2024

@pantsmasterson you need to get rid of the quotation mark (') before and after the code. Or maybe you didn't install the templater plugin?

@pantsmasterson
Copy link

pantsmasterson commented Mar 21, 2024

I put the ticks in the comment so they'd appear as code in the Github comment -- they don't appear in the template itself. In the template, it is exactly as it appears in the screenshot, and renders as that piece of code rather than the cursor at that position (which I assume is what's supposed to happen). In the templater code, I've cut and pasted exactly what appears above. The Templater plugin is installed and active.

@Gryn23
Copy link

Gryn23 commented Mar 21, 2024

  • Did you tick "automatic jump to cursor" in Templator's options?
  • And maybe a stupid question, but you pasted '<% tp.file.cursor() %>' in your template, right? It's supposed to stay code in your template, and only when you 'create new note form template' it should give you a note without the code and just the cursor below your Notes heading.

@pantsmasterson
Copy link

Aha! It was automatic jump to cursor! Thank you so much -- I'd never have puzzled that through solo.

@Gryn23
Copy link

Gryn23 commented Mar 21, 2024

Haha, glad to help, I'm a beginner myself.

@dannberg
Copy link
Author

You're not the first person with a question about <% tp.file.cursor() %>. I should be explicit about this being a setting that needs to be enabled in the post. Will update!

@re3factor
Copy link

re3factor commented Mar 21, 2024

Anyone with a tip to stop odd formatting issue when trying to put in a block of code? Whenever I put in the code formatting ticks it opens code for everything below it, even after putting in the code in question and closing the formatting correctly?

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment