Get list of files that are created between two dates

Hey guys, I’m here to share my approach of getting files created during two dates, it ​can be used to replace the daily activity plugin, which does not have the ability to specify a certain date.

First you need to install both Templater and Dataview plugins.

Then add a new template (e.g. files_created_at.md), and paste the following code in:

<%*
/* config */
const IGNORE_DIR_REGEXS = [
  /^40 Daily Notes/,
]

/* utils */
const linkTailRegex = /\.md\|[^\]]+/
const removeLinkTail = s => new String(s).replace(linkTailRegex, '')

/* main logic */
const dateFormat = 'yyyy-MM-dd'
const dv = this.DataviewAPI
let startDate = await tp.system.prompt('Start date', dv.date('today').toFormat(dateFormat))
if (!startDate) {
  return
}
startDate = dv.date(startDate)
let endDate = await tp.system.prompt('End date (optional)')
console.log('endDate', endDate)
if (endDate) {
  endDate = dv.date(endDate)
} else {
  endDate = startDate.plus(dv.duration('1 day'))
}
console.log('dv', dv, startDate, endDate)

tR += `Files created at ${startDate.toFormat(dateFormat)}~${endDate.toFormat(dateFormat)}:\n`
const out = dv.pages()
  .where(p => {
    if (IGNORE_DIR_REGEXS.filter(v => v.test(p.file.path)).length > 0) {
      return false
    }
    return p.file.ctime >= startDate && p.file.ctime < endDate
  })
  .map(p => {
    return `- ${removeLinkTail(p.file.link)}`
  })
  .join("\n")
tR += out
%>

Now you can insert the template in the editer, just call the command Templater: Open insert template modal, or assign a keyboard shortcut to this template:

The template requires to input a start date, which is by default today in YYYY-MM-DD format:
image

The end date argument is optional, if omitted, it’ll be the day next to start date:
image

The results is like:
image

The code was also posted in the related issue of Dataview

2 Likes