What I’m trying to do
I’m trying to display the total number of files (inside of an specific folder (e.g.: Library) and with an specific tag (e.g.: #Book)) created during the last month. Something like: “In the last month you have added [insert number] books in this library.”
Things I have tried
I’m just a newbie with all the stuff related with JS, but I’ve tried to get the total number of books added in April (for example) with this inline JS that I got somewhere:
`$= dv.pages('"Library" and #Book').filter(page => new Date(page.file.created).getMonth() === 3).length`
For some reason it displays “0” (I actually added some books during that month) but either way I’m not looking for an specific month. This is for a dashboard, so I’d like to display the number independently on the month we are.
Is there a solution?
In case it helps, every book contains the following inline field:
Created:: <% tp.file.creation_date("DD-MM-YYYY HH:mm") %>
That date format is not a recognised date format. Dataview is particular about which format it accepts, and that is the
YYYY-MM-DDTHH:mm format when including hours and minutes.
So you’re trying to create a date based upon a “non-date” field, and that just doesn’t work.
The proper way to solve this is to start using a proper date format (aka ISO8601, as shown above), or do some date mangling using moment.js to parse your date-string into a proper date, or to do some regex-trickery to lift out the month from your date-string.
Using the implicit field
file.cday works in local tests.
In the last month you have added `$= dv.pages('"Library" and #Book').filter(page => new Date(page.file.cday).getMonth() === 3).length` books in this library.
Yes, it worked with
file.cday without changing anything.
If anyone else reads this and has this doubt I’ll use for monthly reports:
`$= dv.pages('"Library" and #Book').filter(page => new Date(page.file.cday).getMonth() === 3).length`
And for the dashboard (last month, but not specified):
`$=dv.pages('"Library" and #Book').filter(page => new Date(page.file.cday) > new Date(Date.now() - 30 * 24 * 60 * 60 * 1000)).length`
Thank you both for the help!!
file.cday is an actual date, this one should suffice:
`$= dv.pages('"Library" and #Book').filter(page => page.file.cday.month == 3).length`
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.