[TWC] DatePlugin question

61 views
Skip to first unread message

jpt

unread,
Jun 23, 2015, 11:00:35 AM6/23/15
to tiddl...@googlegroups.com
Hello,
Using  a suggestion that was made in this group I am able to display a tiddler titled with a specific date ("20150623" for example) by writing this:
<<tiddler {{(newDate()).formatString ('YYYY0MM0DD')}}>>
However I can not figure out how to format it so it displays a tiddler with a date other than today, e.g. tomorrow's date.
I also tried the DatePlugin and wrote this:
<<tiddler {{showDate(Y M D+1 "YYYY0MM0DD"}}>>
but it gives me an error message.
What am I doing wrong?
Best,
Johannes

Eric Shulman

unread,
Jun 23, 2015, 12:12:57 PM6/23/15
to tiddl...@googlegroups.com
I'll answer the second part first:
* In TWC, macros don't generate text output.  They generate and render DOM elements.  Thus, you cannot use the output of a macro as a parameter of another macro.
* In TW5, macros are used to assemble text output and the returned value can, with syntax limitations, be used as a parameter of another macro.

Now on to the real tech:

"new Date()" returns the current date/time in milliseconds since midnight on Jan. 1, 1970 (the start of time, computationally speaking!).  As you've learned, this value can then be formatted using .formatString ('YYYY0MM0DD').

To use a date other than the current one, pass the number of milliseconds into the Date() function.  For example, if you use new Date(0).formatString(...), you will see "19700101".  (note: the date is adjusted for your local time zone, so if you are west of UDT, the start date will show as "19691231")

You can calculate a new value from the current date/time value by adding/subtracting the appropriate number of milliseconds.  For example, one day is 86400000 milliseconds (60 x 60 x 24 x 1000).  Thus, to get tomorrow's date, you would use "new Date() + 86400000" as the input param, like this:

new Date(new Date() + 86400000).formatString('YYYY0MM0DD 0hh:0mm:0ss');

enjoy,
-e
Eric Shulman
ELS Design Studios
TiddlyTools - "Small Tools for Big Ideas!"
InsideTiddlyWiki: The Missing Manuals

YOUR DONATIONS ARE VERY IMPORTANT!
HELP ME TO HELP YOU - MAKE A CONTRIBUTION TO MY "TIP JAR"...

Professional TiddlyWiki Consulting Services...
Analysis, Design, and Custom Solutions:

jpt

unread,
Jun 23, 2015, 1:32:54 PM6/23/15
to tiddl...@googlegroups.com
Thank you.
I understand now that in TWC macros generate and render DOM elements and cannot be used as parameters for other macros.
For whatever reason I still have trouble with the code.
When I write <<tiddler {{(new Date(new Date() + 86400000).formatString('YYYY0MM0DD')}}>> it gives me "Error in macro <<tiddler>>".
To try to see if I missed something I used the WriteIt Plugin and all of the following lines display correctly:
<<writeIt {{(new Date()).formatString('YYYY0MM0DD')>> gives "20150623" (today's date);
<<writeIt {{(new Date(0)).formatString('YYYY0MM0DD')>> gives "19691231" (as mentioned in the answer);
<<writeIt {{(new Date(0 + 86400000)).formatString('YYYY0MM0DD')>> gives "19700101".
However when I write:
<<writeIt {{(new Date(new Date() + 86400000)).formatString('YYYY0MM0DD')>> I get "NaNNaNNaN".
What did I miss?
Regards,
Johannes

Eric Shulman

unread,
Jun 23, 2015, 2:01:39 PM6/23/15
to tiddl...@googlegroups.com
On Tuesday, June 23, 2015 at 10:32:54 AM UTC-7, jpt wrote:
For whatever reason I still have trouble with the code.
When I write <<tiddler {{(new Date(new Date() + 86400000).formatString('YYYY0MM0DD')}}>> it gives me "Error in macro <<tiddler>>".
What did I miss?

You have an extra open parentheses, just before the first "new".
Note also, when you see the red "error in macro", if you click on the message, it shows more details.

-e

jpt

unread,
Jun 23, 2015, 7:04:59 PM6/23/15
to tiddl...@googlegroups.com
Dear Mr Shulman,
Thank you very much!
It got me in the right direction.
Still was not able to get tomorrow's tiddler displayed no matter that the error message disappeared.
Until I read somewhere to put a "+" in front of the second "new Date". 
Like so: <<tiddler {{new Date(+ new Date() + 86400000).formatString('YYYY0MM0DD')}}>>
Seems that this "new Date" was interpreted as something other than a number (which would explain the NaN output).
Can that be or is something else wrong and the "+" handling it is just a fluke?
It now works perfectly and I can add and subtract days as I please.
Thank you very much.
Johannes


On Tuesday, June 23, 2015 at 11:00:35 AM UTC-4, jpt wrote:
Reply all
Reply to author
Forward
0 new messages