Dataview sort note by date DD-MM-YYYY

Things I have tried

I have tried searching how to sort date by DD-MM-YYYY but mostly people use YYYYMMDD convention.

currently I’m using this command

SORT default(date, “”) desc

But, they sort it by day with highest value, which takes precedent before month and year.

What I’m trying to do

How do I sort my daily notes by date in YAML with the format DD-MM-YYY?

Hi.

Two points:

  1. One thing is the format you use as date value, ie, the value in your field. About this, you need to use a format recognized by dataview. Dataview uses the format ISO8601, ie, YYYY-MM-DD.... Data Annotation - Dataview So, the point is: to be a format recognized by dataview you need to use this form.

  2. Other thing is the format you want to see in the output results. If you want to see dd-MM-yyyy then you need to ‘convert’ the original format to the wanted output. For example, if in date date: 2022-05-03 and you want to see in the output 03-05-2022, then you use the function dateformat() in this way:
    dateformat(date, "dd-MM-yyyy")
    using the luton format tokens - luxon/formatting.md at master · moment/luxon · GitHub

1 Like

Apologies to @skymaverik for asking an additional question.

For the films I watch, I have an inline field of:

DateWatched:: 2022-05-03-Tuesday

In my table of films, I have:

TABLE WITHOUT ID
file.link AS Film,
DateWatched AS When

Which lists all the films by year, month, date, day.

If I change this to…

TABLE WITHOUT ID
file.link AS Film,
dateformat(DateWatched, "dd-MM-yyyy") AS When

… it doesn’t work.

I can use…

TABLE WITHOUT ID
file.link AS Film,
DateWatched AS When,
dateformat(file.cday, "DDDD") AS Date

…but as there is often a discrepancy between the DateWatched and the file’s creation date, this method isn’t precise.

What am I doing wrong? Does the query fail because my inline field includes the day of the week?

Thanks

Angel

You give the answer.

As said before, one thing is the right format to be recognized as date, other thing is the output format.

The recognized format has the form YYYY-MM[-DDTHH:mm:ss.nnn+ZZ] (as mentioned in documentation, “everything after the month is optional”.)

In your example, 2022-05-03-Tuesday is recognized as a simple string, not a date. Usually the default output of a date (defined in Settings > Dataview) take the form of MMMM dd, yyyy, something like May 3, 2002 (you can test the output with the inline query =date(today)).

There’s only one implicit field that accept other words and extract only the date - the field file.day (for titles > you can use [[2022-05-03 My daily note]] and file.day extract only the date).

To work normally, your field need to be like this: DateWatched:: 2022-05-03.
But you can work with that value in a no-normal way, using some regex to extract everything from the last “-”, i.e., removing “-Tuesday” (with the function regexreplace()) and then use the function date() to transform the string 2022-05-03 in a recognized date).

I’m a dumb in regex (not only), but you can try something like this:

date(regexreplace(DateWatched, "-[^-]*$", ""))
1 Like

Ah, complete fluke that I got the answer.

Everything makes total sense now that you have explained it to me. As ever, I am very grateful. I will try with regex or perhaps just use VS Code to remove the days of the week from the strings so that they then work in line with the documentation.

Thank you very much.

Angel

EDIT:

Thanks, @mnvwvnm, for this super-handy tip:

you can test the output with the inline query =date(today)

Mine reads: Today is: 2022-05-03-Tuesday

Maybe you’ve edit the default format in settings.

I use a lot simple inline queries to check the right format of the fields in the current note. Something like =this.field
And to check all metadata in current note in structured way I use this one (inline js query):

`$=dv.span(dv.current())`
1 Like

Thanks for those additional tips.

Indeed I had changed the default format. I like to see the day of the week and I had understood that everything after the month was optional. I had also assumed that that meant that anything after the month was also permissible, but I clearly misunderstood.

Despite lots of attempts, I can’t figure out where to place the regex in the query, and even though I tried to edit the dates to yyyy-MM-dd format in the inline fields, I can’t then get Dataview to display those dates in Day-dd-MM-yyyy or even dd-MM-yyyy format in the table.

So I have decided to find a book to read and to come back to this tomorrow.

I can read the dates as they are (backwards to my brain). It would just be nice to be able to read them more naturally. Think it is easier to stick with the simple system I have now and continue to enjoy things rather than get frustrated.

Have a lovely evening.

Angel

I’m not sure about your desired output (and please correct my regex if wrong - I copied and edit it from other code… I really don’t know what means each code character :slight_smile: ):

TABLE WITHOUT ID
	file.link AS Film,
	date(regexreplace(DateWatched, "-[^-]*$", "")) AS Test,
	dateformat(date(regexreplace(DateWatched, "-[^-]*$", "")), "dd-MM-yyyy") as When,
	dateformat(date(regexreplace(DateWatched, "-[^-]*$", "")), "dd-MM-yyyy-cccc") as When2
WHERE DateWatched

… and good reading.

1 Like

Many thanks for the help, yet again. In awe of your knowledge and kindness.

I promise I will never harangue you with questions again.

Muito obrigado

Angel

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