Rename photos calculating the age of the person

I'm wanting to create a button that allows me to rename photos with the age of my children, taking into account the datebirth and the datetaken.

Example: Esther sleeping.jpg > Esther sleeping (1a 3m 5d).jpg

I have the following code (JScript) but it gives me an error in line 1, could someone help me figure out what it is?

Function OnClick( ByRef ClickData )
    let birthdate = "2022-08-21"; //Birthdate
    var birth = new Date( birthdate ).getTime();
    var photo = new Date( {datetaken|D#yyyy-MM-dd} ).getTime();
    let diff = new Date( photo - birth );
    diff.setFullYear( diff.getFullYear() - 1970 );
    let age = name + " ( " + diff.getFullYear() + "y " + diff.getMonth() + "m " + diff.getDate() + "d )." + extension;

    Rename IGNOREEXT PATTERN * TO "* ({age})"

End Function

What's the error?

Error in line 1, position 10
Function OnClick( ByRef ClickData )

Where are you using that code? In a toolbar button, or in the Rename dialog, or somewhere else?

In a toolbar button

That's VBScript.

Change to VBScript and now the error is on line 2

And what is the error?

Actually, I can see the error. You're mixing VBScript, JScript and internal commands all together in a single script. You have to pick a language and then use it consistently, or nothing is going to work.

I understand @Leo , I don't know much about programming, I'll try to correct it, thank you very much, to @Jon too!

Can someone help me please?

Please link your account for more detailed help.

Now it does not generate an error, but the age it calculates is incorrect. For example, in the code it is specified as birthdate: 2023-08-08, if I select a photo with datetaken: 2023-08-09 (1 day apart), the result should be:

Esther(0y 0m 1d).jpg and is being Esther (-1y -1m -30d).jpg

function OnGetNewName(getNewNameData) {
    var birthdate = new Date(Date.UTC(2023, 08, 08, 00, 00, 00));

    var item = getNewNameData.item;
    var metadata = item.metadata;
 
    if (metadata && metadata.image && metadata.image.datetaken) {
        var datetaken = new Date(metadata.image.datetaken);
        var diff = datetaken - birthdate;

        var years = Math.floor(diff / (365.25 * 24 * 60 * 60 * 1000));
        var months = Math.floor((diff % (365.25 * 24 * 60 * 60 * 1000)) / (30.44 * 24 * 60 * 60 * 1000));
        var days = Math.floor((diff % (30.44 * 24 * 60 * 60 * 1000)) / (24 * 60 * 60 * 1000));

        var age = "(" + years + "y " + months + "m " + days + "d)";

        var result = item.name_stem + " " + age + item.ext;
        return result;
    } else {
        return item.name;
    }
}

As a professional photo organizer, I'd like to offer you some advice that has nothing to do with Directory Opus.
I recommend that you do not rely on using filenames for information about a photo. Filenames can get changed by online services, improper sharing and other ways too.
I recommend renaming the photos starting with the Date Taken in the format YYYY-MM-DD. (and while that filename could get changed, it's re-creatable because it's based on metadata, so nothing would be 'lost')
By renaming as I recommend, the photos will sort the same way whether sorted by Date Taken or by Filename. After YYYY-MM-DD, I typically add my client's last name. You could use Esther in the filename, but I recommend that only if Esther is also added as a Tag (Keyword). See below.
Assuming you know your child's birth date (I hope you do), you will know hold old they were (at least approximately) in any given photo by looking at the filename or the Date Taken.
With your current renaming idea, photos would be chronologically scrambled if the files were sorted by filename.
Consider adding 'Esther' as a Tag (Keyword). You could also add 'sleeping' as a Tag (Keyword) but Keywords should be word that you would really search for. So, if you were my client, I wouldn't add the Keyword 'sleeping'.
Consider adding 'Esther sleeping (1a 3m 5d)' to the Description field.

Hi @MegMac Thank you very much for the recommendations, but I don't have that problem with online services, I don't use any; and I would really like to be able to calculate the age automatically. Thank you very much!

You're welcome.
While you may not have that problem, I recommend things that will future-proof the collection. Someone else (like Esther) might do something that causes filenames to change.

You are right, security is everything

@Leo, help me, please, what is the error with date formats?

Please link your account for more detailed help.

Ok, I'm going to by the program and link to the account Dasota