Skip to content
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

Format filter to create formatted date strings #4785

Merged
merged 2 commits into from
Sep 24, 2020

Conversation

saqimtiaz
Copy link
Member

This PR adds an optimized version of a filter I have been using for some time to allow easy creation of formatted date strings. While there are workarounds possible using the view widget and wikify, this extends the ability of filters and is often more convenient.

Operand: The operand to the filter operator is used as date format, consistent with the handling in the View widget. If the operand is not specified it defaults to YYYY MM DD 0hh:0mm which is again consistent with the view widget.

Suffix: If the suffix relative is used, the operand (if any) is ignored and the string is formatted as a relative date.

An invalid date string returns no result.

Examples:
[[20200731]asdate[mmm DD]] -> Jul 31
[[20200730]asdate:relative[]] -> 45 hours ago
[[20200730]asdate:relative[mmm DD]] -> 45 hours ago

If there is interest in adding this to the core I can work on docs as well.

@pmario
Copy link
Member

pmario commented Jul 31, 2020

Does it allow all the possibilities as described here: https://tiddlywiki.com/#DateFormat ?

@pmario
Copy link
Member

pmario commented Jul 31, 2020

Imo also related to: #4784

@saqimtiaz
Copy link
Member Author

Does it allow all the possibilities as described here: https://tiddlywiki.com/#DateFormat ?

Yes the support is complete and the filter relies on the same code from $tw.utils as the view widget, so there is no discrepancy in the handling.

Imo also related to: #4784

I do not think that should impact whether we want to merge this or not. The correct way to implement #4784 for backwards compatability would be to adapt the underlying methods for date handling in $tw.utils such as $tw.utils.formatDateString, in which case the filter would receive those improvements automatically.

@saqimtiaz
Copy link
Member Author

@Jermolene any thoughts? I don't have a vested interested in having this in the core beyond the perceived utility of such a filter to users. If we decide not to merge, I am happy to bundle this as a plugin for those that need it.

@pmario
Copy link
Member

pmario commented Aug 6, 2020

I think it would be useful in the core. We do have a lot of requests in the GG and the existing "workaround" is highly sophisticated wikitext, which is hard to remember.

@Jermolene
Copy link
Member

Thanks @saqimtiaz this is useful. I've also got an implementation of formatDate() as a filter somewhere in my backlog. I took the approach of calling the operator "format", with "date" being one of the suffixes (e.g. [[20200731]format:date[mmm DD]]), but then I don't think I managed to come up with any other types that would be useful. Perhaps format:currency[]?

@saqimtiaz
Copy link
Member Author

@Jermolene apologies for the late reply, I had a bunch of things come up unexpectedly at work that are occuping my attention.

I favour your idea of a format operator with a suffix, it is a more versatile approach and will allow us to supports other formats as needed, without needing new operators each time.

I think date and relativedate are two good suffixes to support at first.

If you have the code for this conveniently at hand already then it would be great to get it posted/merged, otherwise I can rework this PR to implement a format filter with date and relativedate as supported suffixes.

@Jermolene
Copy link
Member

Hi @saqimtiaz I'm afraid I couldn't find the old code; I do usually keep deleted code but not in this case it seems. There wasn't much to it, I think $:/core/modules/filters/format.js was pretty much a copy of $:/core/modules/filters/is.js.

@saqimtiaz
Copy link
Member Author

@Jermolene no worries. I'll rework this PR in that direction sometime this week.

@Jermolene
Copy link
Member

Thanks @saqimtiaz

@saqimtiaz
Copy link
Member Author

@Jermolene @pmario PR updated with a format filter operator (modelled on is operator) and suffixes date and relativedate.

@saqimtiaz saqimtiaz changed the title asdate filter to create formatted date strings Format filter to create formatted date strings Sep 22, 2020
@Jermolene Jermolene merged commit 72c07a3 into TiddlyWiki:master Sep 24, 2020
@Jermolene
Copy link
Member

Thanks @saqimtiaz -- docs would be appreciated when you get a chance.

@saqimtiaz
Copy link
Member Author

Thanks @saqimtiaz -- docs would be appreciated when you get a chance.

Will do. I had just created an issue to put this on my to-do list when I saw this :)

@Jermolene
Copy link
Member

Thanks @saqimtiaz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants