[TW5] 'Countdown to event' macro [Resolved]

218 views
Skip to first unread message

Hegart Dmishiv

unread,
Dec 30, 2015, 10:18:12 PM12/30/15
to tiddl...@googlegroups.com
Is there some way to implement a 'countdown to event' function using TW5's macro functions and fields, rather than using Javascript? I've seen @Tobi's scary enable-js script to allow plain Javascript <script> tags inline on tiddlers, and noted the warning there, so I don't want to do that, thanks all the same. I've also seen @SimonBaird's ReminderMacros, which doesn't seem to do what I wanted, and is for TWC only, anyway. I would like to be able to add a countdown timer onto a tiddler, such as with the simple text example output below...

There are 144 days until Lubuntu 16.04 LTS (Xenial Xerus) is release.

I'd ultimately like to set this up as a MOTD style banner at the top of my wiki, as a stimulus. I haven't yet delved into learning TW5's macro language. Is there a repository I can go to, to grab and modify something to suit my needs? I can follow the logic of this tutorial for creating a countdown timer in Javascript. I don't want to be "that person" who always relies on the kindness of others to create code for me. I can do some of the work myself, as long as it's fairly straightforward. Despite my age, "script kiddie" is an appropriate description for me. ;-)

Hegart Dmishiv

unread,
Jan 1, 2016, 8:51:06 PM1/1/16
to TiddlyWiki
Okay, I've made an attempt at doing this myself. With my serious lack of programming skills, I've cobbled together something from the TiddlyWiki core module $:/core/modules/macros/now.js and the tutorial I was looking at earlier. I didn't need the hours, minutes and seconds information, so I dropped all that guff. For now I went with only making a single call to the macro on loading a tiddler, rather than trying to make it dynamically updating at the top of the wiki instance as a MOTD. It seemed simpler to just at least display the number of days, once, statically.

However, I'm getting a big red Internal Javascript Error dialog box, which says "ReferenceError: assignment to undeclared variable DaysToDate". Could some kind Javascript guru take a look at my first teetering steps and let me know where I went wrong please?

Also, I have no idea of the format to use when calling the macro. What I'm trying presently is <<DaysToDate dd/mm/yyyy>> but I'm not sure what date format it will take. Of course, the preexisting Internal Javascript error hampers my efforts so far to quantify this. ;-)

Jed Carty

unread,
Jan 2, 2016, 3:45:26 AM1/2/16
to TiddlyWiki
I made some macros that may be helpful for this here http://inmysocks.tiddlyspot.com/#Date%20Macro%20Demos

Eventually I may get around to making them better, but that probably won't be for a while.

Hegart Dmishiv

unread,
Jan 2, 2016, 9:36:45 AM1/2/16
to TiddlyWiki
Thanks for the reply Jed. I see you're passing in integers and turning them into a date within the macro. Can't we just have a parameter which is of date type?

BJ

unread,
Jan 2, 2016, 2:30:41 PM1/2/16
to TiddlyWiki
more like this

exports.run = function(target_date) {
 
    // find the amount of seconds between now and target date
    var end_date = new Date(target_date).getTime();
    var current_date = new Date().getTime();
    var seconds_left = (end_date - current_date) / 1000;
 
    // calculate days left, based on 86,400 seconds per day
    var DaysToDate = parseInt(seconds_left / 86400);
return DaysToDate;
};


})();

all the best

BJ

Hegart Dmishiv

unread,
Jan 2, 2016, 4:36:32 PM1/2/16
to tiddl...@googlegroups.com
That was the missing piece, thanks BJ. Now I can call the macro with <<DaysToDate "mm/dd/yyyy">> and it returns the number of days difference. Not too shabby! Thanks everyone for your help with this. I'll implement it as is for now, and maybe later I'll come back to it, to see about turning it into a dynamic, ticking countdown timer thingy to put at the top of my wiki.

I've set a string implementing my new <<DaysToDate>> macro as the SiteSubtitle on my live (offline) TW5 instance, and it works there perfectly. Very happy with this result.
Reply all
Reply to author
Forward
0 new messages