Skip to content

Is it possible to specify a format when displaying a date? #362

Answered by blacksmithgu
timwilson asked this question in Q&A
Is it possible to specify a format when displaying a date? #362
Apr 20, 2021 · 19 answers · 8 replies

I might have missed this somewhere, but I can't find an option to specify a format when displaying a date. For example, when showing a field called date-finished that holds an ISO-formatted date, I don't need the day of the week included. Showing Oct 3, 2020, for example, would be fine.

Is it possible to specify the output format for a date somewhere?

Custom date formats for dataview have been added in 0.4.3.

Replies

19 suggested answers
·
8 replies

That would just be a great addition indeed! Preferrably in some format already known, like maybe the one "Templater" uses? Or simply moment.js?

It should also respect the locale Obsidian is set to, not just the system locale, like Templater does. For instance, when I set Obsidian to "English" on a German system, I get things like "Saturday" and "March" from Templater, but still "März" (March) from dataview.

0 replies

I use luxon for my date handling internally, which essentially the same API as moment.js with an immutable data wrapper and a few other niceties.

I think I might just look at how Templater handles it's dates and go from there. Will probably add a default global date format as a setting, as well as a format function like `dateformat(date, "<some format string/key/whatever>").

0 replies

I second/third/fourth this, it would be great.

Currently = this.dueDate returns e.g. Fri, Apr 16, 2021 automatically, but doing something like =choice(this.dueDate, ..., ...) requires manually building the date and only returns numerics so its quite cumbersome.

Instead of dateformat() another option would be date(...).format(...) which seems nicer and avoids nested parens in situations where date(...) has to be used.

0 replies

I'm doing this hack to keep it short and sweet due-date.month + "/" + due-date.day

0 replies

I'd love to be able to select data output in ISO 8061 format. One use case: often I will cut-and-paste the output into other contexts (like a spreadsheet), and then ISO 8061 is really much more convenient for sorting the text. Plus, its unambiguous.

0 replies

This has gotten easier with the new dataviewjs code blocks (I simply use moment.js in them).

It’d still be nice (and easier for many) if something like birthday.format("YYYY-MM-DD") could be used, ideally in = …, dataview and dataviewjs code. Where .format() without a param string should return some sensible default, like the 2021-05-07T14:23:41+02:00 format moment.js uses. (Also ISO-8061, and I love it.)

Maybe Luxon also offers this?

0 replies

Thanks for the reply, @Moonbase59, but I've never worked with dataviewjs, so I'd appreciate a simple example, e.g., for this query, which I put in every Daily Note, to return links to notes created or last modified on the date (which is the Daily Note's filename, in YYYY-MM-DD)

TABLE file.mday AS "Last Modified", file.cday AS "Date Created"
WHERE (file.mday = this.file.day) OR (file.cday = this.file.day)
SORT file.mtime asc
0 replies

Well, to get you primed, here’s your example translated to dataviewjs, with a little extra glitz:

  • desired dateformat can be taken from YAML front matter (dateformat: 'YYYY-MM-DD HH:mm')
  • I used file.mtime and file.ctime instead, to give more granularity in case you show the time.

For more, please consult the Docs, or check out my DataviewJS Snippet Showcase in the Obsidian Forums.

Result:

obsidian-dataviewjs-comparison

Code

// default dateformat in case it’s forgotten in front matter
var dateformat = "YYYY-MM-DD";
if (dv.current().dateformat) { dateformat = dv.current().dateformat; }

// the table
// I used mtime & ctime instead of mday & cday, for better granularity.

dv.table(["File", "Last Modified", "Date Created"],
  dv.pages()
  .where(p => moment(p.file.mtime.toString()).isSame(dv.current().file.day.toString(), 'day') ||
    moment(p.file.ctime.toString()).isSame(dv.current().file.day.toString(), 'day'))
  .sort(p => p.file.mtime, 'asc')
  .map(p => [
    p.file.link,
    moment(p.file.mtime.toString()).format(dateformat),
    moment(p.file.ctime.toString()).format(dateformat),
  ])
);

HTH, have fun!

EDIT: Missed that you wanted files from the same day as the file’s date in the title, sorry. Updated, now using moment.jsisSame(…, 'day'). Mainly used that because neither p.file.mday == dv.current().file.day nor p.file.mday === dv.current().file.day ever went true here (DV 0.3.5).

0 replies

Are DataviewJS date comparisons not working? Ah, I guess they wouldn't, since it's using JavaScript comparisons. You can use p.file.mday.equals(dv.current().file.day), which should work. Sadly I don't think I can fix that behavior.

0 replies

Thanks so much to both of you! Integrating the simplification from @blacksmithgu, I've now got the following, which gets much just what I was looking for.

`

// default dateformat in case it’s forgotten in front matter
var dateformat = "YYYY-MM-DD";
if (dv.current().dateformat) { dateformat = dv.current().dateformat; }

dv.table(["File", "Last Modified", "Date Created"],
  dv.pages()
  .where(p => p.file.mday.equals(dv.current().file.day) || p.file.cday.equals(dv.current().file.day))
  .sort(p => p.file.mtime, 'asc')
  .map(p => [
    p.file.link,
    moment(p.file.mtime.toString()).format(dateformat),
    moment(p.file.ctime.toString()).format(dateformat),
  ])
);
0 replies

This works well for ctime and mtime, but I can't get it to work for date type fields I have in my files. There doesn't seem to be a toString() for date type fields, and passing it directly into moment() results in today's date. Anyone can help?

0 replies

Hi there, I'm new to dataview and have no JS experience, so am a bit lost in this thread. I would like to get today's date (i.e., changing each day) in YYYY-MM-DD format, ideally as a one-liner.

Currently I have
= date(today)

Which outputs: Mon, Jun 28, 2021

How might I get this instead as 2021-06-28?
From what I gather on this thread, there is no date(today).format(...) and I have to use JS, but I'm lost as to what to write.

Thank you, community!

0 replies

Custom date formats for dataview have been added in 0.4.3.

5 replies
@blacksmithgu

You can also use dateformat(date, "format") to do this on a per-field basis.

@BrunoGomesCoelho

Awesome! If helpful to anyone else, here's luxons formatting docs: https://moment.github.io/luxon/#/formatting

@blacksmithgu

Thanks for adding the link - luxon is similar to moment but has a few different operators.

Answer selected by M-bot
0 replies

@blacksmithgu Thanks ever so much for adding dateformat()!

0 replies
1 reply
@blacksmithgu

You are almost correct:

```dataview
TABLE dateformat(ProjectDeadline, "yyyy-mm-dd") AS "Deadline"
WHERE ProjectDeadline >= date(today)
WHERE ProjectDeadline <= date(today) + dur(2 months)
SORT ProjectDeadline ASC
```

Essentially, you want the selected column to be a computed row, which you then give a name using AS.

0 replies

This comment was deleted.

2 replies
@blacksmithgu

What is yielding null? number(split(this.file.name, "W")[1]) - 1? Does surrounding it parenthesis help?

@brimwats

sorry—didn't mean to actually put this here!

I stumble upon this post and learned about dateformat. However, this function is not mentioned in API doc.

0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue