Need dataviewjs to list ALL files created between Today 5AM to Tomorrow 5AM (epoch time question)

Here is the code I have so far

let date_today = "2024-05-07" // probably add this by hand?
let date_start = "2024-05-07 05:00AM" //pseudo add 5 hours to date_today and convert to epoch time 
let date_end = = "2024-05-08 05:00AM" //pseudo add 29 hours and convert to epoch time

const today_files = app.vault.getFiles().filter(file => file.stat.ctime >= date_start && file.stat.ctime <= date_end );
  
today_files.forEach(file => dv.paragraph(`[[${file.path}]]`))
  • Since I need all files to show up (pdf, jpg, pxc, etc…) I need to use dataviewjs, so no pure dataview.
  • My issue is with Epoch Time. *.stat.cdate uses Epoch Time. And I need this value to filter when file was created.
  • My rough idea is:
  1. Insert manually a date in date_today (ex: 2024/05/07)
  2. A function in date_start uses date_today and add 5 hours, also converts it to epoch time.
  3. A function in date_end uses date_today and add 29 hours, also converts it to epoch time.

I’ve tried googling a lot on how to convert dates to epoch time, but I had no success. I also tried to search the forums and only found pure dataview solutions.

Thank you!

I was able to figure things out by myself, so I suppose I’ll share here. I was also able to take it one step further with a script I found on reddit.

So, first, here is the code you need to just list all files created between 5am today, and 5am tomorrow:

```dataviewjs
   // just in case you don't want it to run always, as it can be intensive.
	let run = true
	if (run == true) {
	console.log("running write data to file");
	
	let date_today = "2024-05-08" // your date go here
   // add 5 hours and 29 hours respectively.
    let date_start = new Date(date_today).getTime()+(5*60*60*1000);
	let date_end = new Date(date_today).getTime()+(1*29*60*60*1000);
	
   // filter files, if you want to know what data you can use for filtering, just do a console.log(app.vault.getFiles())
	const today_files = app.vault.getFiles().filter(file => 
    file.stat.ctime >= date_start && 
    file.stat.ctime <= date_end );

   //write the output to a variable, will display it as a callout.
    let finalText = ">[!embed]- today's links \n"	  
	today_files.forEach(file => finalText += "> - [[" + file.path +"]] \n");
    
   // if you are just using dataviewjs
    dv.paragraph(finalText);

   // if you are using dataviewjs + custom script - more in thread -
    // await dv.view("location/to/your/script", { target: "A", msg: finalText})
}

If all you’re looking for is the dataviewjs solution, just use it as is. The caveat is that the output is always dynamic, it never gets written to your file and only exists when you open the file.

The script I’ve found, on the other hand, can write down to your file. Here you can read more about it:
script link - obsidian forum

the script is by xDerJulien who is a quite nice fellow!

A general walkthrough on how to use it is the following:

  • create a new folder in your vault, like scripts/obsidian/write_to_file
  • inside of this folder you’ll paste xDerJulien script in a new file called view.js (it’s the first code block in his thread)
  • go back to my script and right at the end replace the “location/to…” with your script folder location, don’t do /view.js at the end, nor put the last /.
  • in your document, you’ll have to insert %%INSERT A%% %%END A%% , this will tell the script where to write the data out.

It does seem like this script and make.md had some issues in the past, but not sure as I don’'t use make md anymore.

1 Like