Is it possible to specify a format when displaying a date? #362
-
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 Is it possible to specify the output format for a date somewhere? |
Beta Was this translation helpful? Give feedback.
Replies: 20 comments 10 replies
-
That would just be a great addition indeed! Preferrably in some format already known, like maybe the one "Templater" uses? Or simply 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. |
Beta Was this translation helpful? Give feedback.
-
I use 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>"). |
Beta Was this translation helpful? Give feedback.
-
I second/third/fourth this, it would be great. Currently Instead of |
Beta Was this translation helpful? Give feedback.
-
I'm doing this hack to keep it short and sweet |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
This has gotten easier with the new It’d still be nice (and easier for many) if something like Maybe Luxon also offers this? |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply, @Moonbase59, but I've never worked with
|
Beta Was this translation helpful? Give feedback.
-
Well, to get you primed, here’s your example translated to
For more, please consult the Docs, or check out my DataviewJS Snippet Showcase in the Obsidian Forums. Result: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 |
Beta Was this translation helpful? Give feedback.
-
Are DataviewJS date comparisons not working? Ah, I guess they wouldn't, since it's using JavaScript comparisons. You can use |
Beta Was this translation helpful? Give feedback.
-
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. `
|
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
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 Which outputs: How might I get this instead as Thank you, community! |
Beta Was this translation helpful? Give feedback.
-
Custom date formats for dataview have been added in 0.4.3. |
Beta Was this translation helpful? Give feedback.
-
Awesome!
|
Beta Was this translation helpful? Give feedback.
-
@blacksmithgu Thanks ever so much for adding |
Beta Was this translation helpful? Give feedback.
-
Thanks. My apologies for not being up to speed with the context of the
function.
So how should a query like this work?
```dataview
table ProjectDeadline
dateformat(ProjectDeadline, "yyyy-mm-dd")
WHERE ProjectDeadline >= date(today)
WHERE ProjectDeadline <= date(today) + dur(2 month)
SORT ProjectDeadline ASC
```
…On Sat, 28 Aug 2021 at 13:20, blacksmithgu ***@***.***> wrote:
You use it like other Dataview functions - for example, dateformat(file.ctime,
"yyyy-mm-dd").
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#362 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ARTR6N7RT5JAU5AU2L3DSSTT7BIXTANCNFSM5BH3QADQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
Beta Was this translation helpful? Give feedback.
-
Much appreciated, but I had to change it to "yyyy-MM-dd" because the month
in lower case was rendered as 00. Weird.
…On Sun, 29 Aug 2021 at 18:26, blacksmithgu ***@***.***> wrote:
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.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#362 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ARTR6N6Q22ILEPJLEVNY2WLT7HVJRANCNFSM5BH3QADQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
Beta Was this translation helpful? Give feedback.
-
I stumble upon this post and learned about |
Beta Was this translation helpful? Give feedback.
-
Is there a way to do relative date formatting? Such as (5 minutes ago) |
Beta Was this translation helpful? Give feedback.
Custom date formats for dataview have been added in 0.4.3.