Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
timwilson opened this issue Apr 20, 2021 · 12 comments
Closed

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

timwilson opened this issue Apr 20, 2021 · 12 comments
Milestone

Comments

@timwilson
Copy link

@timwilson timwilson commented Apr 20, 2021

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?

@Moonbase59
Copy link

@Moonbase59 Moonbase59 commented Apr 22, 2021

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.

@blacksmithgu
Copy link
Owner

@blacksmithgu blacksmithgu commented Apr 24, 2021

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>").

@davecan
Copy link

@davecan davecan commented Apr 25, 2021

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.

@misaxi
Copy link

@misaxi misaxi commented May 1, 2021

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

@autonomygaps
Copy link

@autonomygaps autonomygaps commented May 6, 2021

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.

@Moonbase59
Copy link

@Moonbase59 Moonbase59 commented May 7, 2021

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?

@autonomygaps
Copy link

@autonomygaps autonomygaps commented May 11, 2021

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

@Moonbase59
Copy link

@Moonbase59 Moonbase59 commented May 16, 2021

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).

@blacksmithgu
Copy link
Owner

@blacksmithgu blacksmithgu commented May 16, 2021

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.

@autonomygaps
Copy link

@autonomygaps autonomygaps commented May 17, 2021

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),
  ])
);

@mkasu
Copy link

@mkasu mkasu commented May 26, 2021

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?

@maia-sh
Copy link

@maia-sh maia-sh commented Jun 28, 2021

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!

Repository owner locked and limited conversation to collaborators Jul 29, 2021
@M-bot M-bot closed this Jul 29, 2021
@blacksmithgu blacksmithgu added this to the 0.4.3 milestone Jul 30, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
9 participants