How to convert a string to an object in dataviewjs?

Specific goal: To get the ctime or cday of a file, when I only have the path (ie, a string and not an object) to that file.

Details: I want to collate the list items from several daily pages. I’m approaching it from a “tasks” mindset. For example

var myCurrentPage = dv.pages('"reviews/daily"');
const myArrayTasks = myCurrentPage.file.tasks

And then I do some text processing with each task in the myArrayTasks.

I see that there are “implicit fields” (listed on Metadata on Tasks and Lists - Dataview) such as:

item.tags
item.path

which are:
a list of 1 element: “#books
a string “/reviews/daily/2022-12-28”

What I can’t figure out is how to get the date of the page which holds a particular list item. I have a string of the path, but to get the ctime or cday of that path, I think I have to convert it to an object. (Right? Maybe that’s an incorrect assumption.)

But the example given in the Dataview documentation about constructors doesn’t work for me:
https://blacksmithgu.github.io/obsidian-dataview/reference/functions/#objectkey1-value1

Constructors

Constructors which create values.

object(key1, value1, ...)

Creates a new object with the given keys and values. Keys and values should alternate in the call, and keys should always be strings/text.

object() => empty object
object("a", 6) => object which maps "a" to 6
object("a", 4, "c", "yes") => object which maps a to 4, and c to "yes"

My simple test

object("a",6)

gives an error which returns no search results:

Evaluation Error: ReferenceError: object is not defined

Things I have tried

I tried searching on “Evaluation Error: ReferenceError: object is not defined” and tried creating an object with the JSON method, but I keep getting errors.

Maybe a better approach is to iterate over each page in a range of dates, but I couldn’t figure that out, either.

It seems like you want the dv.page() function which fetches the page object of a string path.

Thanks holroy.

I’m still seeing some different behavior from iterated variables passed into dv.page() and hard-coded values passed into dv.page().

var myTestPage2 =  dv.page(item.path); // handle several task items in a loop
     
 // print what Obsidian is doing...

   dv.el('h1',  "F...<br> "  
+ " name " +  myTestPage2.file.name    
 
 // is the daily page "reviews/daily/2022-12-28"  
+ " cday " + myTestPage2.file.cday 
+ " ctime" +  myTestPage2.file.ctime 

+ " name " +  dv.pages('"Pilot"').file.name   // is a regular page
+ " cday "  + dv.pages('"Pilot"').file.cday 
+ " ctime "  + dv.pages('"Pilot"').file.ctime 

  // and here is a daily page, reviews/daily/2022-12-11" 
 // explicitly given and not handled in a loop

+ " name " +  dv.pages('"reviews/daily/2022-12-11"').file.name
+ " cday "  + dv.pages('"reviews/daily/2022-12-11"').file.cday 
+ " ctime "  + dv.pages('"reviews/daily/2022-12-11"').file.ctime 

What is printed:

name 2022-12-28
cday 1672207200000
ctime1672259117424NaN

name [Pilot]
cday [2022-12-28T00:00:00.000-06:00]
ctime [2022-12-28T15:03:36.305-06:00]

name [2022-12-11]
cday [2022-12-11T00:00:00.000-06:00]
ctime [2022-12-11T11:12:06.624-06:00]

What I don’t understand

  1. Where are the brackets coming from? When a value is explicitly passed to dv.pages(), the result is in brackets, and the typeof is ‘object’. When the value comes from a loop and from item.path and is sent to dv.page(), the results are not in brackets. Does this come from a difference between dv.page() and dv.pages()? (Singular vs plural?)
  2. What is the date format for the dv.page-object “1672207200000”? I tried dateformat() and date() but I get errors, perhaps they only work in dataview queries, not dataviewjs.

Thanks for any pointers.

dv.pages() returns potentially a list of pages, and is mostly equal to the ordinary queries. dv.page() is used to get a single page, not multiple pages.

So the brackets typically comes from the result being a list of a single item.

The longish number could very well be the milliseconds since epoch, and is a common way to represent dates in a single number. It can be used to represent the date, and can be formatted to the other variants.

Thanks Holroy! I got it – it was indeed milliseconds since epoch. (I counted the number of digits and searched on “13-digit time”…)

Here’s a verbose look at my script snippet:

 var myTestPage2 =  dv.page(item.path);
 var myTimePageUpdatedInEpochSeconds = myTestPage2.file.ctime;
 var myTimePageUpdatedReadable = dv.func.dateformat(
      dv.date(myTimePageUpdatedInEpochSeconds), "yyyy-MM-dd"
      );

Gives:

2022-12-30

:slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.