Date output format in dataviewjs

What I’m trying to do

I am struggling to format date outputs from dataviewjs

I have a frontmatter field eventdate e.g.

eventdate: 2023-06-20

The default output format is as June 20, 2023 - I want the output to be 2023-06-20

Every option I have tried either errors or produces no error but all dates are replaced by today’s date (see commented attempts below)

    .map(k => [
		k.eventdate,
		//moment(k.eventdate.toString()).format(`YYYY-MM-DD`), 
		//moment(k.eventdate).format('YYYY-MM-DD'),
                //k.eventdate.format('YYYY-MM-DD'),

Things I have tried

Tried extensive searches and can’t find a solution that works.

A more complete dataviewjs block I use is below

```dataviewjs
let pages = dv.pages('"01 Areas/10 Money/00 Money Log"') .where(p => p.mstodo == "1" || p.latextodo == "1" || p.otheraction == "1"); 

for (let group of pages.groupBy(b => b.bank)) {
  dv.header(3, group.key);
  dv.table(["Date", "Who", "Description", "P", "L", "M", "O", "Link"],
    group.rows
      .sort(k => k.eventdate, 'asc')
      .map(k => [
		k.eventdate,
        k.who,
        k.description, 
        k.priority,         
        k.latextodo,
        k.mstodo,
        k.otheraction,
        k.file.link 
      ]
    )
  );
}

Obsidian has its own version of a date object, which cannot be directly passed to the moment-constructor. But here is one way to do it:

    .map(k => [
		moment(new Date(k.eventdate.c.year, k.eventdate.c.month -1, k.eventdate.c.day)).format('YYYY-MM-DD')

Thank you – that looks very interesting - albeit too complex for me to debug. For me that yields an error

Evaluation Error: TypeError: Cannot read properties of undefined (reading ‘c’)
at eval (eval at (plugin:dataview), :22:31)
at Array.map ()
at Proxy.map (plugin:dataview:8038:39)
at eval (eval at (plugin:dataview), :18:8)
at DataviewInlineApi.eval (plugin:dataview:18370:16)
at evalInContext (plugin:dataview:18371:7)
at asyncEvalInContext (plugin:dataview:18381:32)
at DataviewJSRenderer.render (plugin:dataview:18402:19)
at DataviewJSRenderer.onload (plugin:dataview:17986:14)
at e.load (app://obsidian.md/app.js:1:631421)

At least when the suggested code is used as below (perhaps not as intended). Many thanks for the help.

.map(k => [
		moment(new Date(k.eventdate.c.year, k.eventdate.c.month -1, k.eventdate.c.day)).format('YYYY-MM-DD'),
        k.who,
        k.description, 
        k.priority,         
        k.latextodo,
        k.mstodo,
        k.otheraction,
        k.file.link 
      ]

Hmm ok. I think the error happens because of the map-function. It tries to convert the eventdate-field on every page to a new Date. So when a page doesn’t have a value in eventdate (or the value is not a valid date) then this error occurs.

Does this code below help?

.map(k => {
	let eventdate = "no date";
	if (k.eventdate != null) {
		eventdate = moment(new Date(k.eventdate.c.year, k.eventdate.c.month -1, k.eventdate.c.day)).format('YYYY-MM-DD');
	}
	return [
		eventdate,
        k.who,
        k.description, 
        k.priority,         
        k.latextodo,
        k.mstodo,
        k.otheraction,
        k.file.link 
      ]];
}

A quick note - the code I just pasted does not check if eventdate is a valid date. If you have some files with for example eventdate: random text then that could also lead to an error. So it could be worth adding an if-statement to check if the field is a valid date

You don’t have anything which verifies that eventdate actually exists in your result set in your query. This means that there is a likelihood of not set in which case k.eventdate.c.year will trigger error messages like the one you’ve quoted.

In other words, one of your files don’t have a set eventdate. To fix this you would either do a query to locate the offending files, or change the query so that the map only occurs if it’s set. A final option is doing k.eventdate?.c.year (maybe just the question mark without the period), which will allow for event date to be null, which would then render the whole expression as null. (However, this fix might result in other error messages, or that todays date is returned)

So I would try either of the two first options.

The date format can be set in the settings for the dataview plugin.

2 Likes

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