Calculate date difference (date diff)

335 views
Skip to first unread message

Danielo Rodríguez

unread,
May 30, 2017, 5:22:34 AM5/30/17
to TiddlyWiki
Hello,

Searching through TW documentation and the poor and horrible search mechanism of google groups I was unable to find a concrete answer to my problem.

I want to calculate the difference between two date fields. One called start, and one called end, which represents a time lapse when something (represented by a tiddler) happened. 

Seems that there is no macro or widget to accomplish such simple task.

Any guidance will be highly appreciated

Danielo Rodríguez

unread,
May 30, 2017, 5:25:53 AM5/30/17
to TiddlyWiki
Another funny fact is that there is no way to generate a string with the same value as created or modified fields. The closest thing is the now macro, and it is unable to return milliseconds, so there is no way to generate an equivalent created field.

PMario

unread,
May 30, 2017, 5:47:23 AM5/30/17
to TiddlyWiki
On Tuesday, May 30, 2017 at 11:22:34 AM UTC+2, Danielo Rodríguez wrote:
Seems that there is no macro or widget to accomplish such simple task.

:) ... Date calculation is very very far from simple!!

Just naming 3 things that will cause headache:
 
 - leap years
 - time zones
 - summer / winter time

And there is a lot more. ... If you want to do it right. ... use a 3rd party library eg: moment.js

Implementation can be found http://kixam.github.io/TW5-datePicker/ The minified version is about 50k but in the long run it will pay back to use their api calls.

have fun!
mario

Danielo Rodríguez

unread,
May 30, 2017, 1:30:56 PM5/30/17
to TiddlyWiki
Hello Mario ,

I just want to know the number of minutes elapsed, which should be simple. Just a difference between the milliseconds then and now.

Regards

PMario

unread,
May 31, 2017, 8:24:48 AM5/31/17
to TiddlyWiki
Hi Danielo,

How do you create "start" and "end" ... Are they created by a program, or by user input?
How do / should they look like?

-m

Danielo Rodríguez

unread,
May 31, 2017, 10:52:35 AM5/31/17
to TiddlyWiki
Hello Pmario.

It is created using the NOW macro. It does not matter how it looks like, as long as it is easy to calculate the time diff in minutes between them.
I wanted to avoid creating a custom JS macro, but if it is the only way... 

Stephen Kimmel

unread,
May 31, 2017, 3:48:03 PM5/31/17
to TiddlyWiki

It is created using the NOW macro. It does not matter how it looks like, as long as it is easy to calculate the time diff in minutes between them.
I wanted to avoid creating a custom JS macro, but if it is the only way... 

Danielo,

Perhaps something like this would serve as a starting place for what you want...

(function(){
/*\
 Calculates the difference between two dates

<<datediff date1 date2 days>>

\*/


exports
.name = "datediff";

exports
.params = [
{ name: "date1" },
{ name: "date2"},
{ name: "units"},
];

/*
Run the macro
*/


exports
.run = function(date1,date2,units) {

// If no date supplied use current date
if (date2=="") {
var d2=new Date();
var date2 = d2.toLocaleDateString();
}
if (date1==""){
var d1=new Date(5/1/1984);
}

var d1 = new Date(date1);
var d2 = new Date(date2);

switch(units){
case "weeks":
       
var t2 = d2.getTime();
       
var t1 = d1.getTime();

result
= Math.floor(parseFloat((t2-t1)/(24*3600*1000*7))*100)/100+" weeks";
break;
case "minutes":
       
var t2 = d2.getTime();
       
var t1 = d1.getTime();
result
= parseInt((t2-t1)/(60*1000)) + " minutes";

break;
case "months":
       
var d1Y = d1.getFullYear();
       
var d2Y = d2.getFullYear();
       
var d1M = d1.getMonth();
       
var d2M = d2.getMonth();
       
var d1D = d1.getDate();
       
var d2D = d1.getDate();

result
=Math.floor(((d2M+12*d2Y+d2D/30)-(d1M+12*d1Y-d1D/30))*100)/100 + " months";
break;
case "years":
       
var d1Y = d1.getFullYear();
       
var d2Y = d2.getFullYear();
       
var d1M = d1.getMonth();
       
var d2M = d2.getMonth();
       
var d1D = d1.getDate();
       
var d2D = d1.getDate();

result
=Math.floor(((d2M/12+d2Y+d2D/365)-(d1M/12+d1Y-d1D/365))*100)/100 + " years";

// result=parseFloat(d2.getFullYear()-d1.getFullYear()) + " years";
break;
default:
       
var t2 = d2.getTime();
       
var t1 = d1.getTime();
result
= parseInt((t2-t1)/(24*3600*1000)) + " days";
}

return result;
};

})();


 

Danielo Rodríguez

unread,
Jun 1, 2017, 1:23:53 PM6/1/17
to TiddlyWiki

Hello Stephen


Danielo,

Perhaps something like this would serve as a starting place for what you want...

Thanks for providing such macro. However, I wanted to avoid the usage of javascript macros, and I wanted to create a pure tiddlywiki version.
I really think that tiddlywiki, being advertised as something you can use as a diary, having journal facilities and that stuff, should provide better tools for date management.

Finally I made a very simple macro that fits my needs. However, I'm a bit disappointed with this situation.

In any case, I save your macro for future reference.
Regards

Jeremy Ruston

unread,
Jun 1, 2017, 1:37:54 PM6/1/17
to tiddl...@googlegroups.com
Hi Danielo

Thanks for providing such macro. However, I wanted to avoid the usage of javascript macros, and I wanted to create a pure tiddlywiki version.
I really think that tiddlywiki, being advertised as something you can use as a diary, having journal facilities and that stuff, should provide better tools for date management.

The fact that there is no support for date arithmetic in the core is because nobody has contributed it; in order for it to be implemented somebody has to do the work of writing the code, adding tests, checking it on different browsers etc. The fact that that hasn’t happened yet doesn’t mean that nobody thinks it’s a good idea, nor that anyone thinks it’s a bad idea. Asserting that TiddlyWiki “should” have a particular feature has very little impact

Finally I made a very simple macro that fits my needs. However, I'm a bit disappointed with this situation.

Great, then you’ll be in a good position to be the contributor that plugs this gap!

I’d suggest don't waste time being surprised or disappointed; just jump in and help where you can.

Best wishes

Jeremy.

Stephen Kimmel

unread,
Jun 1, 2017, 1:41:34 PM6/1/17
to TiddlyWiki


On Thursday, June 1, 2017 at 12:23:53 PM UTC-5, Danielo Rodríguez wrote:

Thanks for providing such macro. However, I wanted to avoid the usage of javascript macros, and I wanted to create a pure tiddlywiki version.
I really think that tiddlywiki, being advertised as something you can use as a diary, having journal facilities and that stuff, should provide better tools for date management.

Finally I made a very simple macro that fits my needs. However, I'm a bit disappointed with this situation.

In any case, I save your macro for future reference.
Regards

Ah. I misread your post. My mistake.

I would be interested in seeing your macro when you're ready to share it.

Stephen

Danielo Rodríguez

unread,
Jun 2, 2017, 4:08:00 AM6/2/17
to TiddlyWiki
Hello Jeremy
 

The fact that there is no support for date arithmetic in the core is because nobody has contributed it; in order for it to be implemented somebody has to do the work of writing the code, adding tests, checking it on different browsers etc. The fact that that hasn’t happened yet doesn’t mean that nobody thinks it’s a good idea, nor that anyone thinks it’s a bad idea. Asserting that TiddlyWiki “should” have a particular feature has very little impact

I have that fact in mind. However, thank you for clarifying that it is not intentionally left out.
 

I’d suggest don't waste time being surprised or disappointed; just jump in and help where you can.

Maybe disappointed was not the proper word, sorry about that. I think I made clear several times that I really love tiddlywiki and I find it a very valuable tool. I never invest so much time on it otherwise. I don't think neither I'm the kind of person that only complains. I contribute as much as I can, and If I don't do it more it's because lack of confidence.

Of course if I find a general solution for this absence I will contribute with a pull request.

Regards


Danielo Rodríguez

unread,
Jun 2, 2017, 4:09:12 AM6/2/17
to TiddlyWiki
Hello Stephen


 
Ah. I misread your post. My mistake.

Please don't apologize. You have provided a very valid response.
 
I would be interested in seeing your macro when you're ready to share it.

My macro is very biased and focused on my single problem. Is just one line of code, so it's nothing general. In that regard, your solution is much more shareable.

Regards 

Mark S.

unread,
Jun 2, 2017, 3:56:41 PM6/2/17
to TiddlyWiki
Am I right in thinking that a first step in adding date handling abilities to TW5 might be to add a "TW5" format to the <<now>> so that it returns the current stamp exactly like it would (UTC) for the created and modified fields?

Mark

Danielo Rodríguez

unread,
Jun 2, 2017, 4:25:49 PM6/2/17
to TiddlyWiki
Hello Mark,

I think that could be a good first step

Jed Carty

unread,
Jun 2, 2017, 4:37:11 PM6/2/17
to TiddlyWiki
I made some date macros a while ago, there are here http://ooktech.com/jed/ExampleWikis/DateMacros/

from what I remember everything works but I got distracted by other projects before polishing them. If anyone wants to take them feel free.

Mark S.

unread,
Jun 4, 2017, 10:42:05 PM6/4/17
to TiddlyWiki
Reply all
Reply to author
Forward
0 new messages