Change dateformat for dataview chart

What I’m trying to do

I want to plot a chart of my employees’ happiness. The data is taken from a weekly file that I create in which I put the following values:

[Date:: 2023-09-05]
[Happiness:: 8]

Now, the name of the file also incorporates the date, e.g. 2023-09-05 - XYZ Weekly. So if you have an idea how I can take the date from the file name instead of having to add a “date” to each file, that would also be appreciated. However, here’s the code I currently use:

const pages = dv.pages('"1  - Product Leadership/Hagen"')
const happinessDates = =>
const happinessValues = => p.happiness).values  
const chartData = {  
type: 'line',
data: {  
labels: happinessDates,
datasets: [{  
label: 'Happiness',  
data: happinessValues,
backgroundColor: [  
'rgba(255, 99, 132, 0.2)'  
borderColor: [  
'rgba(255, 99, 132, 1)'  
borderWidth: 1,
tension: 0.5,
spanGaps: true,
window.renderChart(chartData, this.container)  

The problem now is twofold:

  • First of all, the labels of the chart show the dates with full timestamps, yet I want it to show as YYYY-MM-DD.
  • Second, it seems that the order in the chart is messed up. It shot plot from left to right the oldest to the newest dates. But I get November 2023 first, then September 2023 and then 2024 data etc.

Things I have tried

All kinds of uses of the function dateformat; as well as substringing the date from the file name.

Many, many thanks in advance!!

I’m not sure which part is doing the date formatting in this case, I don’t think it’s dataview (not entirely sure though), so it could possibly be Obsidian or the charts plugin? Not sure… One way to make it display it as is though, would most likely either be to format it as a string (which it sounds like you’ve tried already) (, or possibly to make into a string which is not recognised as a date later on.)

Regarding the sorting, your initial set of pages are not sorted, so the output is most likely reflecting that. And instead of using date you could use given that your filename has a compliant date within it, which you say it do.

So changing the first few lines to this seems to do the trick:

const pages = dv.pages('"1  - Product Leadership/Hagen"').sort(p =>, 'asc')

const happinessDates = =>

At least this variant worked in my simple scenario, but I might have set to use the ISO Date format in general, so please report back if it doesn’t do the trick for you.

Hey Holroy,

thanks a lot for the quick response! The sorting did its job, thanks much for that. As to the formatting, I just get errors once I use the function toISODate as you proposed.

This is what I currently have in the settings of dataview for date format. I even changed the date + time format to only have the year, month and date in desperation - to no avail.

Do you happen to have any other ideas or know what I must change? Also, I realized that I should keep using the date since not every note has a value for happiness (I only ask this about every month).

This is the error I get:

Evaluation Error: TypeError: Cannot read properties of undefined (reading 'toISODate')

That means some of your files don’t have the date in the file name, so the isn’t defined.

Either you need to detect which files that is, and do something with those files, or alternatively you could add a .where(p => in front of the mapping (and thusly remove the files causing the error).

That was it, indeed! Thanks a lot.

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