Reversing the contents of a field

I have a TW with many tiddlers, each having two fields, date-start and date-end, amongst other of course. Each of these fields contains a date in string format, dd/mm/yyyy. I need to sort by either and so have worked out, far tooooooo late, that I should have enterrred these as UTC dates.

I would like to add new fields to the appropriate tiddlers (not all tiddlers have these fields), so that I add sort-date-start and sort-date-end, having the date-start and date-end field contents in reverse order. I have been going through functions, regex, variables, etc, but am getting more confused.

The algorithm is simple (in programming terms),
for date-start, create sort-date-start and add contents from date-start in reverse order, removing the slash characters. for date-end, similar actions.

One thought I had, after creating the new fields (can’t see how to do that through a script though), use 3 variables, dd, mm and yyyy, to extract the related values and then write a single string yyyymmdd into the receiving field.

Can someone guide me out of this morass?

bobj

Have found an explanation as shown.

(https://kookma.github.io/TW-Scripts/

Will give this a try.

Still need to create the additional fields though

1 Like

The overall process is to set up a button, containing a list inside with all the tnings to do for each current tiddler. We would call this a batch conversion.

We can now also use a function to act on the current tiddlers fieldname and split and format the date inside the function (filter)

  • I can give more details if you want.
1 Like

If you really want to add a second field with the same value, something like this would probably do:

<$button>Add sort fields
<$list filter=[tag[MyTag]]>   <!-- Change me -->
  <$action-setfield $field="sort-date-start" $value={{{ [{!!date-start}split[/]reverse[]join[]] }}} />
  <$action-setfield $field="sort-date-end" $value={{{ [{!!date-end}split[/]reverse[]join[]] }}} />
</$list>
</$button>

But I wouldn’t recommend keeping both formats; it’s much better to have a single source of truth. So I would actually replace the current fields with new ones:

<$button>Update date fields
<$list filter=[tag[MyTag]]>  <!-- Change me -->
  <$action-setfield $field="date-start" $value={{{ [{!!date-start}split[/]reverse[]join[]] }}} />
  <$action-setfield $field="date-end" $value={{{ [{!!date-end}split[/]reverse[]join[]] }}} />
</$list>
</$button>

This will involve updating any templates or text fields that display the current dates. I don’t know how many there are, but individually they should be easy.

Mostly by coincidence, this version has a nice feature: you can apply it over and over without changing anything, so if you need to create different filters to handle different groups of tiddlers, you won’t break anything if some tiddlers end up in multiple groups.

You can download the following and drag it on any wiki to test either technique:

RearrangeDates.json (1.4 KB)

1 Like

I have reviewed @Scott_Sauyet suggestion and concur with his view. It is better to retain juts a single start-date and end-date field and use a view template to format the view accordingly or maybe just have the user get used to UTC formatting.

Thanks Scott.

bobj