[TW5] Some simple date macros

425 views
Skip to first unread message

Jed Carty

unread,
Feb 25, 2015, 5:21:50 PM2/25/15
to tiddl...@googlegroups.com
I made some simple date macros that give the time between two dates and one that will add (or subtract) a specific amount of time to the current date.

For some reason whenever I use new Date(); the object returned says it is 22 days ago. This is consistent so the add-time macro manually adds 22 to the day part. I can't figure out why, it should be the same thing as used in the now macro.

It seems like there should be a better way to do this, and hopefully I will add the option to give dates as strings instead of numbers.

Any suggestions on what operations would be useful to have?

PMario

unread,
Feb 25, 2015, 8:09:45 PM2/25/15
to tiddl...@googlegroups.com
hmm,

<<day-diff 2000 2 28 2000 2 29>>   ... 1
<<day-diff 2000 2 28 2000 2 30>> ... 2  ... your description says this is the 1st of March
<<day-diff 2000 2 28 2000 3 1>> ... 3    ... I must have been missing this extra day :)

-m

Jed Carty

unread,
Feb 25, 2015, 8:20:58 PM2/25/15
to tiddl...@googlegroups.com
Yeah, some of the things I have tested since posting that have been missing a few days and stuff like you are showing. I have been trying to figure out why since it is just using the internal javascript date objects, which are what the now macro uses.

PMario

unread,
Feb 25, 2015, 8:25:09 PM2/25/15
to tiddl...@googlegroups.com
Imo there is a rounding problem: 

var days_diff = Math.floor(elapsed/dayMS);

-m

Jed Carty

unread,
Feb 25, 2015, 9:07:47 PM2/25/15
to
Well, one problem is that to go from 1- to 0-based indexing you subtract one not add one like I was doing. So there is that. The macro was treating your example as going from March to April instead of February to March. I fixed that error at least, now to track down all the others.

Thanks for pointing out that specific error, I probably would have taken much longer to figure it out if I wasn't thinking in terms of how long months are.

PMario

unread,
Feb 26, 2015, 2:45:13 AM2/26/15
to tiddl...@googlegroups.com
On Thursday, February 26, 2015 at 3:07:47 AM UTC+1, Jed Carty wrote:
Well, one problem is that to go from 1- to 0-based indexing

I see :) .. I didn't have a look at the code.

If I see date based calculations, I always play with those dates, or at least February. Since it's most likely to be some trouble there ;)

Year 2000 was very special. eg: every 4 years feb + 1 day. every 100 years .. no + every 400 years .. yes :))

Be aware, that calculating the "week of the year" has a different algorithm for US and Europe. ... just an info.

have fun!
mario

Stephan Hradek

unread,
Feb 27, 2015, 2:53:23 AM2/27/15
to tiddl...@googlegroups.com


Am Mittwoch, 25. Februar 2015 23:21:50 UTC+1 schrieb Jed Carty:
I made some simple date macros that give the time between two dates and one that will add (or subtract) a specific amount of time to the current date.

For adding and substracting you might also like to take a look at my datetime macro $:/macros/skeeve/dateTime.js at http://tiddlystuff.tiddlyspot.com The tiddlywiki there is a bit aged but the macro should still work.
Reply all
Reply to author
Forward
0 new messages