Read User Input Date and Show in Correct Format

I have some lazy users they are requested to enter some dates in a tiddler field! They enter dates in different and sometimes ugly mixed format! like

  • 2021.09.5
  • 2021-09-05
  • 2021 09 05
  • 2021/09/05
  • 2021 - 09.5
  • 2021/ 09/ 05

But always they follow year, month and day.

TW has to read all this ugly dates and show them in a nicely format! I want to know what is the simple and TW good programming style

One solution

\define dispdate() 
<$list filter="[<currentTiddler>split[.]trim[]join[]split[-]trim[]join[]split[ ]trim[]join[]split[/]trim[]join[]] "> 
<li><<currentTiddler>> gives  
<$view field=title format=date template="YYYY-0MM-0DD"/>
</li>
</$list>
\end

<$tiddler tiddler="2011    -09 -  24">
<<dispdate>>
</$tiddler>

Reproduce

  • open https://tiddlywiki.com
  • create a new tiddler
  • paste above code as tiddler body
  • change tiddler="2011 -09 - 24" to any data and mixed format
Relates topics

Mohammad,

I saw recently the splitregexp[\s] for whitespaces
You may find a regexp that handles all the cases.

Thanks @TW_Tones. Yes, splitregexp is a better solution!

I used something like below

<$vars pattern="[-.\s\/]">
<$list filter="[<currentTiddler>splitregexp<pattern>!is[blank]]">
...
1 Like

I posted some day ago this

I think that the version for input dates will be something like

<$vars date={{{ [<currentTiddler>splitregexp[\D+]!is[blank]join[-]] }}} >
<<date>>
</$vars>
2 Likes

Hi Alvaro,

Wonderful and clever solution! Thank you!

Now this snippet can handle any kind of delimiter!

1 Like

Yes. But there is a problem if they write the name of the month, it searchs and splits all not digit string with this regexp.

That is right! If so, we need to handle all cases (date validation)! For digits plus any kind of delimiters it works!

A little more;

You could then add the suffix 000000000 and you would have a tiddlywiki date serial number, and view would work on it (but it does on shorter dates as well). However we have seen cases where it’s best to make it 12 noon to help with getting the date + or - 12 Hours so use 120000000 to assist with cross time zones.

Look at the following, and try on the prerelease

<$edit-text tiddler="$:/temp/birthday" tag="input" type=date/>
<$button tooltip="save selected date to bithday-date">
<$action-setfield $field="birthday-date" $value={{{ [{$:/temp/birthday}!is[blank]search-replace:g[-],[]addsuffix[120000000]] }}}/>
<$action-deletetiddler $tiddler="$:/temp/birthday"/>
Save Birthday
</$button>

The above allows you to type in only valid dates, or click to select from a date picker. It then converts it to the tiddlywiki serial number format and saves it in a field.

  • Use the view widget to display in any format.
  • Should respond to your localisation with mm/dd or dd/mm (works for me)

You should check out https://tiddlytools.com/timer.html#TiddlyTools%2FTime%2FConvertDate

Usage: <<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 (see ISO 8601 - Wikipedia]]).
  • The output format uses TiddlyWiki Date Format codes (see https://tiddlywiki.com/#DateFormat), and defaults to “[UTC]YYYY0MM0DD0hh0mm0ss0XXX”.

It handles all the example date formats you listed, except for “2021 - 09.5”… because of the combination of spaces and dashes. If you entered it as “2021-09.5” or “2021 09.5” then it DOES convert!

Note that this macro is just using the built-in javascript date object, which it then passes through the TWCore date formatter. It’s literally just one line of JS code in a TW macro wrapper:

exports.run = function(from,to) {
	return $tw.utils.formatDateString(new Date(from),to || "[UTC]YYYY0MM0DD0hh0mm0ss0XXX");
};

enjoy,
-e

2 Likes

Hi Eric,
This is a complete and flexible solution!

Thank you

Hello Mohammad,
I was wondering if you could add an example of this solution to TWScripts.
Regards,
Scot

Hello Scot,
For a while I did not update the TW-Script! But I have bookmarked this one to be added!
I will publish a partial update and add this one too!

Mohammad, that’s great news.
I look forward to your new edition.

Thanks very much,
Regards Scot

Mohammad see my note for Shiraz here Navigate within a tiddler - #8 by TW_Tones