software:tiddlywiki
This is an old revision of the document!
Creating a baby journal with TiddlyWiki 5
(function(){
"use strict";
exports.name = "age";
exports.params = [ { name: "birthDate" }, { name: "currentDate" } ];
exports.run = function(birthDate, currentDate) {
var getNormalizedDate = function (date, referenceDate) {
var numberOfDaysInMonth = (new Date(date.getFullYear(), date.getMonth()+1, 0)).getDate();
if (numberOfDaysInMonth < referenceDate.getDate()) {
return numberOfDaysInMonth;
}
else {
return referenceDate.getDate();
}
}
var getAgeString = function (birthDate, currentDate) {
var tmpBirthDate = new Date(birthDate);
var tmpCurrentDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate());
if (tmpCurrentDate < tmpBirthDate) return "not yet born";
tmpCurrentDate.setHours(12);
var ageDays = 0;
while (tmpCurrentDate.getDate() != getNormalizedDate(tmpCurrentDate, tmpBirthDate)) {
ageDays++;
tmpCurrentDate.setDate(tmpCurrentDate.getDate() - 1);
}
tmpCurrentDate.setDate(1);
tmpBirthDate.setDate(1);
var ageMonths = 0;
while (tmpCurrentDate.getMonth() != tmpBirthDate.getMonth()) {
ageMonths++;
tmpCurrentDate.setMonth(tmpCurrentDate.getMonth() - 1);
}
var ageYears=0;
while (tmpCurrentDate.getFullYear() != tmpBirthDate.getFullYear()) {
ageYears++;
tmpCurrentDate.setFullYear(tmpCurrentDate.getFullYear() - 1);
}
return ageYears + ' year' + (ageYears == 1 ? '' : 's') + ', ' + ageMonths + ' month' + (ageMonths == 1 ? '' : 's') + ' and ' + ageDays + ' day' + (ageDays == 1 ? '' : 's');
};
if (typeof(currentDate) == "undefined" || currentDate == "" || isNaN(currentDate)) {
currentDate = new Date();
}
else
{
currentDate = new Date(currentDate.substring(0,4), currentDate.substring(4,6) - 1, currentDate.substring(6, 8));
}
if (typeof(birthDate) == "undefined" || birthDate == "" || isNaN(birthDate)) {
birthDate = new Date();
}
else
{
birthDate = new Date(birthDate.substring(0,4), birthDate.substring(4,6) - 1, birthDate.substring(6, 8));
}
return getAgeString(birthDate, currentDate);
};
})();
software/tiddlywiki.1397329889.txt.gz · Last modified: by superwizard
