Today/now as a filter operator

There is a now macro than can be sued to get the current time or date.

But there is no such thing for a filter expression. You can use {{{ [<now>] }}} but you can’t parameter it so it’s not that useful.

To sum up: now is the time for a now filter operator! With the same parameter than the now macro. {{{ [now[YYYY-0MM-0DD]] }}}

This works {{{ [<now YYYY-0MM-0DD>] }}}

as does

\function now.date() [<now YYYY-0MM-0DD>]
\function now.stamp() [<now YYYY0MM0DD0hh0mm0ss0XXX>]

 <<now.date>>
 <<now.stamp>>

This only issue is when you need [UTC]

1 Like

This is not an issue. When used in a filter, the macro parameter values are parsed separately from the filter syntax in which the macro occurs. Thus, square brackets that are within the value of a macro parameter DO work:

{{{ [<now YYYY0MM0DD0hh0mm0ss0XXX>] }}}

{{{ [<now [UTC]YYYY0MM0DD0hh0mm0ss0XXX>] }}}

Also note that if the macro parameter contains spaces you will need to enclose the parameter value within quotes, like this:

{{{ [<now "[UTC]DDD, MMM DDth YYYY 0hh:0mm:0ss.0XXX">] }}}

-e

3 Likes

Yet, there is a problem when you don’t know what format to apply. Say you wants the new Journal tiddler format (stored in $:/config/NewJournal/Title). How would you do that in a filter?

“short-syntax” macros (using <<macroname ...>>) don’t permit transclusion of parameter values, so something like this:

<<now {{$:/config/NewJournal/Title}}>>

won’t work, regardless of whether it is in a filter, or just occurs in regular wikitext. Of course, within wikitext, you can always use the full $macrocall widget syntax, which DOES allow transclusion of parameter values, like this:

<$macrocall $name="now" format={{$:/config/NewJournal/Title}}/>

Unfortunately, you can’t invoke widgets from within filter syntax. However, when using filter syntax there is another way to apply a transcluded datetime format, like this:

{{{ [<now YYYY0MM0DD0hh0mm0ss0XXX>format:date{$:/config/NewJournal/Title}] }}}
  • The <now ...> macro outputs a TWCore standard 17-digit datetime value
  • The format:date{...} filter operator then applies the desired formatting retrieved from $:/config/NewJournal/Title

enjoy,
-e

1 Like

@EricShulman {{{ [<now YYYY0MM0DD0hh0mm0ss0XXX>format:date{$:/config/NewJournal/Title}] }}} only works because you have a fixed format. You can’t replace YYYY0MM0DD0hh0mm0ss0XXX by the content of a variable.

and the following code is not possible:

<$let fmt = {{$:/config/NewJournal/Title}}
today = <$transclude $variable=now format={{$:/config/NewJournal/Title}}/>
>
<$transclude $variable=now format=<<fmt>>/> VS <<today>>
</$let>
>

the output is:

 > 18:24, 8 avril 2024 VS </$let>

BTW, $macrocall is now deprecated, that’s why I used $transclude.

So even in a simple $let sequence, I don’t know how to have my custom format date if my custom format is stored in a tiddler.

That problem is more general that the specific now problem we are speaking of here.

Wrapping the today value in triple-double quotes works:

<$let
      fmt = {{$:/config/NewJournal/Title}}
      today = """<$transclude $variable=now format={{$:/config/NewJournal/Title}}/>"""
>

<$transclude $variable=now format=<<fmt>>/> 
<br>VS<br> 
<<today>>

</$let>

Output:

2024-04-08
VS
2024-04-08

And just plain double-quotes also works, so long as you’re not using them internally for attribute values.

@Brian_Radspinner Yes… it seems to work. But not quite ! See below:

<$let
      fmt = {{$:/config/NewJournal/Title}}
      today = """<$transclude $variable=now format={{$:/config/NewJournal/Title}}/>"""
      got = {{{ [<today>search:title[2024]then[good]else[bad]] }}}
>

; fmt
: <<fmt>>
; today
: <<today>>
; got
: <<got>>

</$let>

output (for me):

fmt
    todo-YYYY0MM0DD
today
    todo-20240408
got
    bad

today’s value is just a formula (the calling of $transclude) not its result. This is why got is bad, when we were would looking for good.

Try this:

<$let fmt={{$:/config/NewJournal/Title}}>
<$wikify name="today" text="<$transclude $variable=now format=<<fmt>>/>">
<$let got={{{ [<today>search:title[2024]then[good]else[bad]] }}}>

; fmt
: <<fmt>>
; today
: <<today>>
; got
: <<got>>
</$let>
</$wikify>
</$let>

If you return to my solution using functions, they have the advantage of being evaluated and all you need to know is how to express something as a filter.

So for example in a function definition [{$:/config/NewJournal/Title}] will return the content of that tiddlers text field and your format. However you may as well as combine it with your use of now like eric’s format.

\function display.today() [<now YYYY0MM0DD0hh0mm0ss0XXX>format:date{$:/config/NewJournal/Title}]
  • Notice how I named the function in general terms, I would consider using this in all my wikis as a global definition, keeping in mind it is always up to date.
  • I use display. to indicate functions designed for display output rather than subsequent calculations. In this case because the output may be hard to parse depending on the format.
  • If you want to use this function in wikitext <<display.today>> you may want to add +[join[]] at the end.
3 Likes

@EricShulman Thank you. I had had the same idea and made it work too. but this solution has the defect of having let/wikify/let instead of a single let. A bit unconvenient and ugly, but it wosks!

Great idea @TW_Tones. Congratulations and many thanks to you!!! I had absolutely not thought of something like that!

My final code is thus:

\function .today(fmt:"YYYY0MM0DD") [<now YYYY0MM0DD0hh0mm0ss0XXX>format:date<fmt>]

\procedure __createToday(today)
  <$action-log $$message=__createToday $$filter=today/>
  <$action-createtiddler $basetitle=<<today>> tags=Journal type="text/vnd.tiddlywiki"/>
\end __createToday
  
<$let
  journalFormat = {{{ [{$:/config/NewJournal/Title}!is[blank]else[todo-YYYY-0MM-0DD] 
  today = {{{ [.today<journalFormat>] }}}
  createToday = {{{ [<today>!tag[Journal]then[__createToday]] }}}
>
  <$transclude $variable=<<createToday>> today=<<today>>/>
</$let>

Good work parametising it further.