Skip to main content
Steve @ Footsteps
Participant
February 29, 2024
Question

Is it possible to calculate a number of years form the DOB and current date?

  • February 29, 2024
  • 1 reply
  • 350 views

I have a document that is regularly updated. It has profiles with the number of years experience. I would like to add a text variable so that each year this number increments by 1 e.g “10 years experience”

This topic has been closed for replies.

1 reply

Community Expert
March 2, 2024

Yes it's possible 

Depending on your setup - I don't know what your layout is like

We need a lot more info from you on what you want to do and how it looks and how you want it to behave.

 

But basically

A script could work like this - you select the date formats you want to change 

whatever your date format might be

dd/mm/yyyy

yyyy-mm-dd

 

etc.

 

Then run the script - it inserts the age

And beside nonconforming date formats it will insert NaN - you probably don't want this

But the drop down lets you decide what your date format is which can be adjusted in the script

It's optional - you can just tell us the date format and we'll put it in that way - or you can adjust yourself

 

I notice it's not working entirely correctly - but can be fixed 

 

 

Run the script

 

 

 

 

It will work on every text frame (should do)

// Function to calculate age from birth date
function calculateAge(birthDate, dateFormat) {
    var today = new Date();
    var birthDateArray;
    switch (dateFormat) {
        case "dd/mm/yyyy":
            birthDateArray = birthDate.split('/');
            break;
        case "mm/dd/yyyy":
            birthDateArray = birthDate.split('/');
            break;
        case "yyyy-mm-dd":
            birthDateArray = birthDate.split('-');
            break;
        default:
            birthDateArray = birthDate.split('/'); // Default to dd/mm/yyyy
            break;
    }
    var birthYear = parseInt(birthDateArray[2], 10);
    var birthMonth = parseInt(birthDateArray[1], 10) - 1; // Month is zero-based
    var birthDay = parseInt(birthDateArray[0], 10);
    
    var age = today.getFullYear() - birthYear;
    var monthDiff = today.getMonth() - birthMonth;
    
    // If birth month hasn't occurred yet this year, or if it's the birth month but the birth day hasn't occurred yet
    if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDay)) {
        age--;
    }
    
    return age;
}

// Function to create a dialog box for selecting date format
function createDateFormatDialog() {
    var dialog = app.dialogs.add({ name: "Select Date Format" });
    with(dialog) {
        with(dialogColumns.add()) {
            var dateFormatDropdown = dropdowns.add({ stringList: ["dd/mm/yyyy", "mm/dd/yyyy", "yyyy-mm-dd"], selectedIndex: 0 });
        }
    }
    var result = dialog.show();
    if (result === true) {
        var selectedDateFormat = dateFormatDropdown.stringList[dateFormatDropdown.selectedIndex];
        dialog.destroy();
        return selectedDateFormat;
    } else {
        dialog.destroy();
        return null;
    }
}

// Main function to insert age at the end of each line
function insertAgesAtEndOfLine(textFrame, birthDates, dateFormat) {
    var lines = textFrame.contents.split("\r"); // Split text by line breaks
    for (var i = 0; i < lines.length; i++) {
        var age = calculateAge(birthDates[i], dateFormat);
        lines[i] += " " + age.toString(); // Add age at the end of each line
    }
    textFrame.contents = lines.join("\r"); // Join lines back together
}

// Function to process all text frames in a document
function processAllTextFrames(document, birthDates, dateFormat) {
    for (var i = 0; i < document.pages.length; i++) {
        var page = document.pages[i];
        for (var j = 0; j < page.textFrames.length; j++) {
            var textFrame = page.textFrames[j];
            insertAgesAtEndOfLine(textFrame, birthDates, dateFormat);
        }
    }
}

// Example usage
var document = app.activeDocument;

// Example birth dates
var birthDates = [
    "03/05/1989",
    "11/15/1976",
    "2000-07-28",
    "1995-02-10",
    "09/22/1982"
];

// Show dialog to select date format
var selectedDateFormat = createDateFormatDialog();
if (selectedDateFormat !== null) {
    processAllTextFrames(document, birthDates, selectedDateFormat);
}

 

Just an example.

Steve @ Footsteps
Participant
March 13, 2024

Thanks Eugene Sorry i’ve not replied sooner i’ve been away form the office.
What i want to achieve is "Mr Smith has been working for 8 years, Mrs Brown had been working for 11 years."
So the 8 and 11 would dynamically update in the indesign document every 365 days. Based on a date I sent for each value

Community Expert
March 13, 2024

You'd have to run the script each time. 

 

What's the base date to calculate the years.

 

With birthdays we can input the date and calculate the age.

 

So we need a start point to do the calculation from somewhere