Rename photos calculating the age of the person

What are datetaken and birthdate when it goes wrong?

Do they match the date the file's metadata shows elsewhere, and the birthday you want?

Those are the first things to check.

The values of both dates are fine, even though they have different UTC. Will that be the problem?
Please @Leo , @bytespiller , @lxp can you help me?

Those two results look fairly consistent to me.

A month has about 4 weeks, so 5 months, 4 weeks and 1 day is quite similar to 6 months and 2 days.

It probably just comes down to what each thing is counting as a month. (Calendar month, or average number of days in a month.) On top of the tool on the left including weeks for values less than a month, while the code on the right could use days.

The UTC offset being different by an hour is probably due to DST being in effect for one date and not the other. Since you're calculating a time in days, it's probably not a problem here, at least unless you plan to take some photos in other timezones.

Anything to do with dates and times always ends up complex like this. :smiley:

I think the code takes the total number of days between the dates and divides it by 365, and gets the years; Divide the remainder by 30 and get the months, and with the remainder, you get the days.

In this way, all the years and months are the same, leap years are not taken into account, nor the variation of days between the months.

Isn't there a way to use a Windows library for this purpose?

Your old code did that, but the code you're using now -- at least what's visible in the screenshot -- is doing it a different way, based on calendar values.

Doubtful anything in Windows would give you more than what you already have here.

Ok, I'm going to keep the current code and settle for that result. :grinning:

I wanted to ask you a question related to the topic, but I'd better ask another question, thank you very much!

In case anyone might be interested, this is the complete code. It works from the advanced renaming window. Eternally grateful to @Leo and @bytespiller!!!

function OnGetNewName(getNewNameData) {
    var birthdate = new Date(2019, 2 - 1, 13);
	
    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 = datetaken.getFullYear() - birthdate.getFullYear();
	var months = datetaken.getMonth() - birthdate.getMonth();
	var days = datetaken.getDay() - birthdate.getDay();
	
	var strYears = (years != 0) ? (years + "a ") : "";
	var strMonths = (months != 0) ? (months + "m ") : "";
	var strDays = (days != 0) ? (days + "d ") : "";
	var strTotal = (strYears + strMonths + strDays).slice(0,-1);
	
	var age = "(" + (strTotal || "0 tiempo") + ")";
	
        var result = item.name_stem + " " + age + item.ext;
        return result;
    } else {
        return item.name;
    }
}
1 Like

The negative figures you get for pictures taken in January and the first half of every month won't bother you?

Oi @lxp, true, I don't want to get those incorrect values, but I haven't known how to do better :thinking:

In this script I have the condition if(diff < 0) return; but I would like it to display some information when it is true. I have tried to replace this line with the following, but it doesn't work, it tells me that dlg is not defined, but I don't know how to do that, can someone help me?!?

if(diff < 0){
dlg.title = "Error";
dlg.message = "Error: Fecha de captura menor que fecha de nacimiento";
dlg.icon = "error";
dlg.buttons = "Aceptar";
dlg.Show();
return;
}

Before you can work with variables, they have to be declared (telling the interpreter that there is a variable that has a name) and most often they have to be initialzed (giving it a value).

var dlg; //declaring the variable
dlg = DOpus.Dlg(); //initializing the variable
or
var dlg = DOpus.Dlg(); //declaring and initializing at once

A dialog object can be created from different classes, but since you are calling from rename script I think you have to go with

var dlg = DOpus.Dlg();

1 Like

Hi @Felix.Froemel thank you very much for your answer, I will incorporate it into the code. I already knew that I had to declare the variable, but my problem was how to declare it, with what? For me, objects and methods is very confusing, but I am going to continue studying and one day I hope to understand it.

@Felix.Froemel Perfect!

2023-08-24 18 23 41

In other languages variables have types (eg for numbers, text, dates...), whis is not the case for dopus, everything is just "var" for the eyes of the developer. Classes are blueprints, objects of classes are "built" from the blueprint and can contain methods for data manipulation. But going more into details would be too much for this place. You can learn a lot from practice, but in my opinion Dopus/ecma scripting might not be the best start (old language, not easy to debug and see whats going on, no real class concept....). But since it is actually ecma script, you can learn a lot from java script (you often have to step back when taking js code to dopus but the basics are the same). So i would advise you to have a look into w3 js schools.

Thank you very much once again, both for your words and for the site that you recommend, I already took a quick look and I liked it, it has the main programming languages, and everything is very well organized, I am certainly going to visit it a lot, big hug!!!