Skip to main content
Participant
May 17, 2025
Answered

Looking for Feedback: Tool to Quickly Calculate Age for Use in InDesign Projects

  • May 17, 2025
  • 1 reply
  • 151 views

Hi everyone,

 

I often work on forms and print materials in InDesign where I need to display a person's exact age (in years, months, days) — especially for official documents, certificates, or medical layouts.

 

To speed up this process, I created a simple web tool that instantly calculates precise age from a birthdate:
https://حسابالعمر.com/

 

It's designed mainly for Arabic-speaking users and works on both desktop and mobile.

I’d love any feedback — and also wondering:
Does anyone here use similar tools alongside InDesign?


Would an InDesign plugin version of this be useful?

Thanks

Correct answer Eugene Tyson

It's nice - had to translate page to English - so what's the goal here?
Are you batch processing them?

Surely for an InDesign project an Excel sheet with calculation would be better - and then you could data merge all the text (possibly)

 

I didn't look at difference in age between two people or how long to next birthday.

 

For age of a person it can definitely be scripted in InDesign (even I can do it)

 

if (app.selection.length === 1) {
    var selected = app.selection[0];
    var textFrame = null;

    if (selected.constructor.name === "TextFrame") {
        textFrame = selected;
    } else if (selected.hasOwnProperty("parentTextFrames") && selected.parentTextFrames.length > 0) {
        textFrame = selected.parentTextFrames[0];
    }

    if (textFrame !== null) {
        var content = textFrame.contents;
        var lines = content.split(/\r|\n/);
        var results = [];

        for (var i = 0; i < lines.length; i++) {
            var line = manualTrim(lines[i]);

            // Regex to capture optional name + mandatory date at end
            var regex = /^(.*?)(\d{1,2}\/\d{1,2}\/\d{4})$/;
            var match = line.match(regex);

            if (match) {
                var namePart = manualTrim(match[1]);
                var dateStr = match[2];

                var dateParts = dateStr.split("/");
                var day = parseInt(dateParts[0], 10);
                var month = parseInt(dateParts[1], 10) - 1;
                var year = parseInt(dateParts[2], 10);

                var dob = new Date(year, month, day);

                if (dob.getFullYear() === year && dob.getMonth() === month && dob.getDate() === day) {
                    var age = calculateAge(dob, new Date());
                    var ageString = age.years + " years, " + age.months + " months, " + age.days + " days";

                    if (namePart.length > 0) {
                        results.push(namePart + " " + ageString);
                    } else {
                        results.push(ageString);
                    }
                }
            }
        }

        if (results.length > 0) {
            var resultString = results.join("\r");

            var newFrame = textFrame.parentPage.textFrames.add();
            var selBounds = textFrame.geometricBounds;
            var frameHeight = selBounds[2] - selBounds[0];
            newFrame.geometricBounds = [
                selBounds[2] + 5,
                selBounds[1],
                selBounds[2] + frameHeight + 5,
                selBounds[3]
            ];
            newFrame.contents = resultString;

            alert("Ages calculated:\r" + resultString);
        } else {
            alert("No valid dates found in the selection.");
        }
    } else {
        alert("Please select a text frame or text inside a text frame.");
    }
} else {
    alert("Please select one text frame or text inside it.");
}

function manualTrim(str) {
    return str.replace(/^\s+/, "").replace(/\s+$/, "");
}

function calculateAge(dob, today) {
    var years = today.getFullYear() - dob.getFullYear();
    var months = today.getMonth() - dob.getMonth();
    var days = today.getDate() - dob.getDate();

    if (days < 0) {
        months--;
        var prevMonth = new Date(today.getFullYear(), today.getMonth(), 0);
        days += prevMonth.getDate();
    }

    if (months < 0) {
        years--;
        months += 12;
    }

    return { years: years, months: months, days: days };
}

 

 

1 reply

Eugene TysonCommunity ExpertCorrect answer
Community Expert
May 17, 2025

It's nice - had to translate page to English - so what's the goal here?
Are you batch processing them?

Surely for an InDesign project an Excel sheet with calculation would be better - and then you could data merge all the text (possibly)

 

I didn't look at difference in age between two people or how long to next birthday.

 

For age of a person it can definitely be scripted in InDesign (even I can do it)

 

if (app.selection.length === 1) {
    var selected = app.selection[0];
    var textFrame = null;

    if (selected.constructor.name === "TextFrame") {
        textFrame = selected;
    } else if (selected.hasOwnProperty("parentTextFrames") && selected.parentTextFrames.length > 0) {
        textFrame = selected.parentTextFrames[0];
    }

    if (textFrame !== null) {
        var content = textFrame.contents;
        var lines = content.split(/\r|\n/);
        var results = [];

        for (var i = 0; i < lines.length; i++) {
            var line = manualTrim(lines[i]);

            // Regex to capture optional name + mandatory date at end
            var regex = /^(.*?)(\d{1,2}\/\d{1,2}\/\d{4})$/;
            var match = line.match(regex);

            if (match) {
                var namePart = manualTrim(match[1]);
                var dateStr = match[2];

                var dateParts = dateStr.split("/");
                var day = parseInt(dateParts[0], 10);
                var month = parseInt(dateParts[1], 10) - 1;
                var year = parseInt(dateParts[2], 10);

                var dob = new Date(year, month, day);

                if (dob.getFullYear() === year && dob.getMonth() === month && dob.getDate() === day) {
                    var age = calculateAge(dob, new Date());
                    var ageString = age.years + " years, " + age.months + " months, " + age.days + " days";

                    if (namePart.length > 0) {
                        results.push(namePart + " " + ageString);
                    } else {
                        results.push(ageString);
                    }
                }
            }
        }

        if (results.length > 0) {
            var resultString = results.join("\r");

            var newFrame = textFrame.parentPage.textFrames.add();
            var selBounds = textFrame.geometricBounds;
            var frameHeight = selBounds[2] - selBounds[0];
            newFrame.geometricBounds = [
                selBounds[2] + 5,
                selBounds[1],
                selBounds[2] + frameHeight + 5,
                selBounds[3]
            ];
            newFrame.contents = resultString;

            alert("Ages calculated:\r" + resultString);
        } else {
            alert("No valid dates found in the selection.");
        }
    } else {
        alert("Please select a text frame or text inside a text frame.");
    }
} else {
    alert("Please select one text frame or text inside it.");
}

function manualTrim(str) {
    return str.replace(/^\s+/, "").replace(/\s+$/, "");
}

function calculateAge(dob, today) {
    var years = today.getFullYear() - dob.getFullYear();
    var months = today.getMonth() - dob.getMonth();
    var days = today.getDate() - dob.getDate();

    if (days < 0) {
        months--;
        var prevMonth = new Date(today.getFullYear(), today.getMonth(), 0);
        days += prevMonth.getDate();
    }

    if (months < 0) {
        years--;
        months += 12;
    }

    return { years: years, months: months, days: days };
}