Adding 'it has been x days since you last did y' to daily notes

This DataviewJS script is intended for users who use daily notes to track various habits or events. It provides an easy way to see how many days have elapsed since a particular condition was last true, useful for habit tracking or monitoring any regular activities.

Requirements for Setup:

  1. Daily Notes Setup: Your daily notes must be named in a consistent “YYYY-MM-DD.md” format.
  2. Specific Folder: All daily notes should be stored in a designated folder, specified in the script.
  3. Frontmatter Property: Each note should contain a YAML frontmatter section with a boolean property that you are tracking (e.g., exercise, meditation, alcohol).

Customization:

  • Replace "YOURDAILIESFOLDER" with the path to your folder containing the daily notes.
  • Change YOURFRONTMATTERNAME to the actual frontmatter property you’re tracking (e.g., exercise).

This script uses Dataview to parse through each note, extracting the dates directly from the filenames. It then calculates how many days have passed since the property was last set to true, providing insight into the consistency of the tracked habit or event.

```dataviewjs
const today = new Date(); // Get today's date

const recentDate = Array.from(dv.pages('"YOURDAILIESFOLDER"') // Add your dailies folder here
    .where(p => p.YOURFRONTMATTERNAME === true) // Add your frontmatter here
    .map(p => new Date(p.file.name))) 
    .reduce((latest, date) => date > latest ? date : latest, new Date(0)); 

const daysSinceLastEvent = (today - recentDate) / (1000 * 3600 * 24); 

dv.paragraph(`It has been ${Math.floor(daysSinceLastEvent)} days since the last recorded event.`);
```

Note: I had chatgpt rewrite the dataview query to be more general (I don’t use YYYY-MM-DD format). Apologies if it doesn’t work.

Duolinguo.jpg LOL