I'm wanting to create code that allows me to rename photos by calculating the person's age.
Example: Esther.jpg > Esther (1a 3m 5d).jpg
To calculate the age, it would take into account the date of birth, incorporated in a fixed way in the code, and the datetaken, obtained from the metadata of the photo.
The following code is executed completely without generating an error, but the result of the years, months and days is expressed in the form of NaN (Not a Number), apparently due to an error in the date formats.
Current result: Esther (NaNy NaNm NaNd).jpg
function OnGetNewName(getNewNameData) {
var birthdate = new Date("2019-02-13T00: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;
}
}
The problem in your code is that the WSH JScript doesn't support ISO string dates for Date initialization, so all your date values (birthdate and datetaken) are NaN. You should initialize the Dates like so: new Date(2019, 2, 13); but be careful I think that the months are zero-based so it may be off-by-one.
Once you've initialized proper dates, the values will be in milliseconds since epoch, and your Math.floor functions should work.
Many thanks to both @bytespiller and @Leo, but I modified the code as per your instructions and the NaN result is no longer displayed, but the calculated age is incorrect.
For example, I select a photo with datetaken 2019-02-14, barely 1 day apart from birthdate (2019-02-13), so the result should be: Esther (0y 0m 1d).jpg and it is being Esther (-1y -1m -27d).
Maybe it's the off-by-one that @bytespiller tells me about, but I don't know how to correct that detail?!?
Try changing new Date(metadata.image.datetaken) to one of these. If one works, the other should make it worse. If both seem to work (or both make it worse) then I might be wrong about it being timezone related.
new Date(metadata.image.datetaken.FromUTC())
new Date(metadata.image.datetaken.ToUTC())
Edit: You could also use Opus's own Date object, instead of the JScripts ones. That should avoid the issue as you'd no longer be mixing the two objects together, and you can compare the y/m/d values separately without all the extra math:
@Leo I don't want to use your kindness, but seeing that it is difficult to solve this situation and that I know so little about programming, couldn't you do me the great favor of creating a completely new code for me, in the way that you find best?
The code may work for me but not you due to the timezone you are in, or the data in the file you're testing against, neither of which I can easily recreate here.
What do you get if you output the two dates like I asked just above?
I inserted the lines in the code and the two dates with different UTC are displayed, and the month of birth in 3 when it should be 2, as said @bytespiller.
The JScript Datemonth values are in range 0-11 (and not 1-12 as you would expect, so the month value 0 means January and the month value 11 means December).
To fix this, simply put the birthdate as: var birthdate = new Date(2019,01,13); or if you prefer: var birthdate = new Date(2019,02-1,13);
Everything almost perfect, correct years and months, but the days have a difference of 7-8 days with reality. I have tried to replace this code with the other one but without success.
var years = datetaken.getFullYear() - birthdate.getFullYear();
var months = datetaken.getMonth() - birthdate.getMonth();
var days = datetaken.getDay() - birthdate.getDay();