convert date in a text field to date format

286 views
Skip to first unread message

unread,
Aug 13, 2021, 3:36:11 AM8/13/21
to TiddlyWiki
Hello forum,

I use a created text field 'published', which will be filled with a date.
(This date can also be in the future)
format is T. MMMM JJJJ - (eg. 7. April 2021 or 13. August 2021).

1. How can this be converted into a date format?

2. The 'published' field is also shown in TOC - $:/_macros/alternative/toc -  (see https://groups.google.com/g/tiddlywiki/c/AAPGy6pFpnc/m/hlKHyhzsAwAJ).
I would like to have TOC sorted on this field (recent on top)

Thanks for feedback
Stefan

Mark S.

unread,
Aug 13, 2021, 2:35:40 PM8/13/21
to TiddlyWiki
I would use a whole bunch of the new search-replace filters. What is your target date format?

unread,
Aug 13, 2021, 4:46:17 PM8/13/21
to TiddlyWiki
Hello Mark,

the displayed field in TOC should be unchanged, as I filled it: T. MMMM JJJJ - (eg. 7. April 2021 or 13. August 2021).

Stefan

Eric Shulman

unread,
Aug 13, 2021, 5:53:09 PM8/13/21
to TiddlyWiki
On Friday, August 13, 2021 at 12:36:11 AM UTC-7 S² wrote:
I use a created text field 'published', which will be filled with a date.
(This date can also be in the future)
format is T. MMMM JJJJ - (eg. 7. April 2021 or 13. August 2021).

1. How can this be converted into a date format?

I have just added a new javascript macro to the TiddlyTools timer package.

  • To convert a date to a different format, use <<convertdate from:inputdate to:outputformat>>.
  • The input date can use any date format recognized by the Javascript Date.parse() function. Generally this conforms to ISO 8601 standard date format.
  • The output format uses TiddlyWiki Date Format codes, and defaults to [UTC]YYYY0MM0DD0hh0mm0ss0XXX.
enjoy,
-e

unread,
Aug 14, 2021, 4:16:43 PM8/14/21
to TiddlyWiki
Hello Eric,

thanks for the new macro, but I have no idea, how to assign it  :-(

Regards
Stefan

Mark S.

unread,
Aug 15, 2021, 7:45:52 PM8/15/21
to TiddlyWiki
Is your source text field in T.MMMM JJJJ format?

Are you converting it back to TW format?

Where are you storing the converted value?



On Friday, August 13, 2021 at 12:36:11 AM UTC-7 S² wrote:

Mark S.

unread,
Aug 15, 2021, 7:47:47 PM8/15/21
to TiddlyWiki
@ Eric

Just tried 

<<convertdate from:"7. April 2021" to:"[UTC]YYYY0MM0DD0hh0mm0ss0XXX">>

and got a hard RSOE ("from" not defined). I think the arguments need to be tweaked in the macro.

HTH

TW Tones

unread,
Aug 15, 2021, 8:08:35 PM8/15/21
to TiddlyWiki
Format  T.MMMM JJJJ ?

Surely you mean "0DD. MMM YYYY"

In this case you would parse this date to extract the DD, MMM and YYYY as separate values, convert from MMM to a month number with a 12 month table, then re-assemble them into a tiddlywiki date/time string. I suggest 1200 or 12 noon as the time, otherwise zero fill seconds and milliseconds.

eg D all before ".", 
Month split[ ]nth[2]
Year split[ ]nth[3] or last[]

Regrds
Tones

Mark S.

unread,
Aug 15, 2021, 8:49:32 PM8/15/21
to TiddlyWiki
On Sunday, August 15, 2021 at 5:08:35 PM UTC-7 TW Tones wrote:
Format  T.MMMM JJJJ ?

Surely you mean "0DD. MMM YYYY"


That's a good point. I imagine S2's source application is using some local formatting code (e.g. German, Dutch). So the format would have to be altered to the ISO 8601 standard for use in javascript (and probably all of TW) when specifying input. But, presumably the output would automatically conform to the browser's locale string ??



 

Eric Shulman

unread,
Aug 15, 2021, 9:12:35 PM8/15/21
to TiddlyWiki
On Sunday, August 15, 2021 at 4:47:47 PM UTC-7 Mark S. wrote:
Just tried 
<<convertdate from:"7. April 2021" to:"[UTC]YYYY0MM0DD0hh0mm0ss0XXX">>
and got a hard RSOE ("from" not defined). I think the arguments need to be tweaked in the macro.

OOPS!  I had originally used "date" and "format" as the parameter names, but then I decided to change to using "from" and "to".  Unfortunately, the arguments in the macro function declaration were still using "date" and "format", but internally, the macro code referenced "from" and "to" as intended.  I've corrected the arguments in the macro function declaration and posted an update here:


Thanks,
-e

unread,
Aug 16, 2021, 6:37:26 AM8/16/21
to TiddlyWiki

Hello,

thanks for all input and effort.
It looks like it is not so easy...

As I wrote, I use the text field ‘publish’ to add a date. (date modified or created will not help me)

This date can also be in the future.
Format is T. MMMM JJJJ - (eg. 7. April 2021 or 13. August 2021) – this is the long date in German (like "DDth MMM YYYY")

'published' is also shown in TOC:

  TOC.png

Now asked for a way to sort TOC on ‘published’ instead of Tiddler title.

The output should still be "DDth MMM YYYY"

Thanks
Stefan

Mark S.

unread,
Aug 16, 2021, 2:31:28 PM8/16/21
to TiddlyWiki
So what you really need is a filter operator that can do the conversion and be used inside a sortsub operator. @Eric ?

If you're willing to use an "update" button, one solution would be a button that creates "published-tw" (date in TW format) fields for any tiddler that has a "published" field. Then you could have the TOC sort on the published-tw field. You would have to remember to periodically perform the "update" whenever you had data changes. This two-step approach makes certain activities in TW much easier to handle.

So the code to update the field might look like:

<$button>Update Publication Dates
<$list filter="[has[published]]">
<$wikify text="""<$macrocall $name=convertdate from={{!!published}} to="[UTC]YYYY0MM0DD0hh0mm0ss0XXX" />""" name=result >
<$action-setfield $field=published-tw $value=<<result>> />
</$wikify>
</$list>
</$button>

Be sure to make a backup before trying this of course. Also, this assumes that you have the latest version of Eric's converdate macro installed.


unread,
Aug 16, 2021, 3:30:13 PM8/16/21
to TiddlyWiki
Mark, thanks for feedback.

The added 'published-tw' field is empty - no values:
published-tw.png

Mark S.

unread,
Aug 16, 2021, 6:57:54 PM8/16/21
to TiddlyWiki

So, did you add Eric's macro to your TW and verify it works? That would be the first step in trouble-shooting. Remember, after loading the convertdate macro, you have to save and reload your TW file.

It worked for me without problem:


Mark S.

unread,
Aug 16, 2021, 7:00:46 PM8/16/21
to TiddlyWiki
screenshot-converted-date.png

unread,
Aug 17, 2021, 4:51:13 AM8/17/21
to TiddlyWiki
Mark,
yes, Eric's macro is there, tagged as ' $:/tags/Macro'.

I downloaded an emty Wiki - 'published-tw' field will also not be filled.

To use the "Update Publication Dates" button is a workaround but not the best solution.

I miss a system 'date' field, like 'created' or 'modified'.
When this field is filled with format "DDth MMM YYYY" than it is a valid date of "[UTC]YYYY0MM0DD0hh0mm0ss0XXX"

BTW: in $:/ControlPanel/Title of new journal tiddlers is the same format (DDth MMM YYYY) defined

One solution can be:
1. Adding a text field (eg 'published')
2. Define, that this field is a date/time field
3. The value of the 'published' field will be converted to "[UTC]YYYY0MM0DD0hh0mm0ss0XXX" and is available as system 'date' field.

Than it can also be used in TOC like
<toc-selective-expandable 'Inhalt' 'sortan[date]' >>

as it is possible for created:
<toc-selective-expandable 'Inhalt' 'sortan[created]' >>

Regards
Stefan
Reply all
Reply to author
Forward
0 new messages