How do I create a custom field with added leading zeros?

87 views
Skip to first unread message

JWHoneycutt

unread,
Aug 24, 2017, 12:24:16 AM8/24/17
to TiddlyWiki
I have a TW5.html file with many tiddlers that start with dates - I did not use zeros for single digit months or dates, but now realize that I would like an order field for sorting.

Example tiddler's title: "2017-8-5 Group meeting"

I now would like to create a field for this tiddler called "order" with the value "20170805"

By adding leading zeros to single digit months and dates I will be able to sort[order] without November and February out of order

How do I accomplish this?

My attempts so far are to filter for all titles with prefix 2017-8-, then remove prefix 2017-8-, then addprefix 2017-08-, then create a matching field (not sure how), then do the same for month 07... and then the days... I don't know how to represent a character variable, and I'm sure there are much more elegant solutions...

Has anybody already dealt with this?

Stephan Hradek

unread,
Aug 24, 2017, 6:52:36 AM8/24/17
to TiddlyWiki
Wouldn't it be easier to fix all the titles?

Stephan Hradek

unread,
Aug 24, 2017, 8:10:54 AM8/24/17
to TiddlyWiki


Am Donnerstag, 24. August 2017 12:52:36 UTC+2 schrieb Stephan Hradek:
Wouldn't it be easier to fix all the titles?


Maybe this will help

\define fixmonth() $(year)$0$(rest)$
\define fixday() $(year)$$(month)$0$(rest)$

<$list filter="[regexp[^\d\d\d\d-\d\d?-\d\d?\b]] +[sort[]]"/>

--

<$list filter="[regexp[^\d\d\d\d-\d-]]" variable="theWrongOne">

<$list filter="[title<theWrongOne>splitbefore[-]]" variable="year">

<$list filter="[title<theWrongOne>removeprefix<year>]" variable="rest">

<$button>
<$action-setfield $tiddler=<<theWrongOne>> $field="title" $value=<<fixmonth>>/>
<$action-deletetiddler $tiddler=<<theWrongOne>>/
>
Fix month for <<theWrongOne>>
</$button>

</
$list>

</$list>

</
$list>

<$list filter="[regexp[^\d\d\d\d-\d\d-\d\b]]" variable="theWrongOne">

<$list filter="[title<theWrongOne>splitbefore[-]]" variable="year">

<$list filter="[title<theWrongOne>removeprefix<year>splitbefore[-]]" variable="month">

<$list filter="[title<theWrongOne>removeprefix<year>removeprefix<month>]" variable="rest">

<$button>
<$action-setfield $tiddler=<<theWrongOne>> $field="title" $value=<<fixday>>/>
<$action-deletetiddler $tiddler=<<theWrongOne>>/
>
Fix day for <<theWrongOne>>
</$button>

</
$list>

</$list>

</
$list>

</$list>

Note that you will delete any duplicates with the above approach! So if you have a tiddler "2017-1-12 and a tiddler "2017-01-01" you will loose the "2017-01-01" and will be left with "2017-1-1" but now named "2017-01-01".

The tiddler above shows one button for each tiddler having one-digit months offering you to fix the month.

It does the same for every tiddler haveing 2 digit month and a one digit day.

Have fun.

Arlen Beiler

unread,
Aug 24, 2017, 1:01:58 PM8/24/17
to TiddlyWiki
Instead of building a widget tree, you could just do a JavaScript macro, couldn't you? 

return date.split('-').map(e=>{
//If e.length === 1, return "0"+e;
//Else return e;
}).join('-');


--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+unsubscribe@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/05079cdd-da24-4250-bb7e-8e2fdd70040a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages