Script/Macro/Plugin to view a persons age?

16 views
Skip to first unread message

Tobbe

unread,
Oct 23, 2009, 7:38:24 AM10/23/09
to TiddlyWiki
Hi,

I'm searching for a method to view a persons age in a tiddler. Let's
say the person whas bord 20 apr 1645 and died 30 nov 1715. How old is
that person in years, month and days? Even if the person is still
alive today it should show the persons present age.

TIA.

//Tobbe

rakugo

unread,
Oct 23, 2009, 8:41:57 AM10/23/09
to TiddlyWiki
Lucky for you I've been working on that for my family tree vertical.
Unfortunately only giving amount of months and years (not days).
The code is below - hope this is a good starting point.
Jon.

config.macros.age ={
handler: function(place, macroName, params, wikifier, paramString,
tiddler){
var d1 = tiddler.fields["dob"];
var d2= tiddler.fields["dod"];
if(!d2){
d2 = new Date().convertToLocalYYYYMMDDHHMM();
}
var y1 = parseInt(d1.substr(0,4));
var y2 = parseInt(d2.substr(0,4));

var m1 = parseInt(d1.substr(4,2));
var m2 = parseInt(d2.substr(4,2));

var day1 = parseInt(d1.substr(6,2));
var day2 = parseInt(d2.substr(6,2));

var ageY = y2-y1;
var ageM = m2 - m1;
if(m2 > m1){
ageM = m2 - m1;
}
else if(m2 < m1){
ageM = 12 -(m1- m2);
ageY -=1;
}

var html =ageY + "years";
if(ageM != 0){
html+=" and "+ ageM+" months";
}
jQuery(place).html(html);

}
};

Eric Shulman

unread,
Oct 23, 2009, 9:33:11 AM10/23/09
to TiddlyWiki
> Lucky for you I've been working on that for my family tree vertical.
> Unfortunately only giving amount of months and years (not days).
> The code is below - hope this is a good starting point.

Here's some other code that also calculates days (approximated using
average of 30.4 days/month):

var now=new Date();
var then=new Date(prompt('enter a date',"1/1/2009"));
var age=now.getTime()-then.getTime();
var hs=3600000; var ds=24*hs; var ms=30.4*ds; var ys=365*ds;
var y=Math.floor(age/ys);
var m=Math.floor((age-y*ys)/ms);
var d=Math.floor((age-y*ys-m*ms)/ds)+1;
return '%0 years, %1 months, %2 days'.format([y,m,d]);

enjoy,
-e

Tobbe

unread,
Oct 23, 2009, 9:36:43 AM10/23/09
to TiddlyWiki
Hi,

Ok :) thanks alot...but,

I'm a novice you know. How do I use it in a tiddler? I've saved the
macro/script as a new tiddler with the name AgeMacro and tagged it
systemConfig (don't know if it has to). Can you give an example?

Thanks again :)

//Tobbe

rakugo

unread,
Oct 23, 2009, 9:56:09 AM10/23/09
to TiddlyWiki
Oh ok sorry!
So yep tag it with systemConfig and it will become a plugin.
At the moment it is hardwired to look for a field called 'dob' and
'dod' on the tiddler it is called from.
These fields must have dates in the format YYYYMMDD.

How are you planning to use it?
Would you want it to work for the tiddler it is called from or would
you want to give it two dates as arguments? If the latter try the
following (untested) by doing <<age 20071201 20080302>>

The first two lines of the macro set where the date comes from. Don't
be scared to experiment by changing where they come from.
Hope my help was better this time :)
Jon

config.macros.age ={
handler: function(place, macroName, params, wikifier, paramString,
tiddler){
var d1 = params[0];
var d2= params[1];
//var d1 = tiddler.fields["dob"];
//var d2= tiddler.fields["dod"];
if(!d2){
d2 = new Date().convertToLocalYYYYMMDDHHMM();
}
var y1 = parseInt(d1.substr(0,4));
var y2 = parseInt(d2.substr(0,4));

var m1 = parseInt(d1.substr(4,2));
var m2 = parseInt(d2.substr(4,2));

var day1 = parseInt(d1.substr(6,2));
var day2 = parseInt(d2.substr(6,2));

var ageY = y2-y1;
var ageM = m2 - m1;
if(m2 > m1){
ageM = m2 - m1;
}
else if(m2 < m1){
ageM = 12 -(m1- m2);
ageY -=1;
}

var html =ageY + "years";
if(ageM != 0){
html+=" and "+ ageM+" months";
}
jQuery(place).html(html);

}

};


Tobbe

unread,
Oct 23, 2009, 2:12:44 PM10/23/09
to TiddlyWiki
> How are you planning to use it?
I put in the birthdate as: 1945-01-01
I put every data in a table (niceTable) as below

{{niceTable{
|Firstname:| Jacob|
|Lastname:| Lewis|
|Born:|1945-01-01|
|Died:|2002-04-21|In the age of <<age 19450101 20020421>>|
}}}


> Would you want it to work for the tiddler it is called from or would
> you want to give it two dates as arguments?
Yes and no :) if the person is still living can I use 1945-01-01
<<today>> as argument?
Or does it have to be two dates?

If the latter try the
> following (untested) by doing <<age 20071201 20080302>>

Thanks again... Eric and Jon, I'll be back ;)

//Tobbe

Tobbe

unread,
Oct 23, 2009, 2:35:08 PM10/23/09
to TiddlyWiki
Hi again,

It's working perfect, thanks a million.

I used it like this:
<<age 19570120 >> and I get the year and month (52 years and 9 month)

Beautiful.... :)

//Tobbe

Eric Shulman

unread,
Oct 23, 2009, 4:04:42 PM10/23/09
to TiddlyWiki
> I'm a novice you know. How do I use it in a tiddler? I've saved the
> macro/script as a new tiddler with the name AgeMacro and tagged it
> systemConfig (don't know if it has to). Can you give an example?

To use the script code I provided, you can import this tiddler:
http://www.TiddlyTools.com/#ShowAge

Then, embed it in your content like this:
<<tiddler ShowAge with: startdate enddate>>

You can specify dates using any javascript-recognized format,
including:
YYYY/MM/DD
MM/DD/YYYY
Month DD, YYYY
etc.

Omitting the end date defaults to the current date. You can also use
'today' as a keyword for either date. Dates can even be in the
future, so that you can write:
<<tiddler ShowAge with: today 2010/01/01>>
to show how long it will be until the next New Year's day.

Also, for your particular use-case, because you have used a *slice
table* to define the birth/death dates, you can even do something like
this:

<<tiddler ShowAge with:
{{store.getTiddlerText(tiddler.title+'::Birth','')}}
{{store.getTiddlerText(tiddler.title+'::Death','')}}>>

enjoy,
-e
Eric Shulman
TiddlyTools / ELS Design Studios

Tobbe

unread,
Oct 23, 2009, 4:35:05 PM10/23/09
to TiddlyWiki
Fantastic Eric :) thanks a million

//Tobbe

shavinder

unread,
Oct 24, 2009, 12:56:23 PM10/24/09
to TiddlyWiki
<<tiddler GreatEnding with:"transclusion">>
Reply all
Reply to author
Forward
0 new messages