Help with baby's age

Advanced Renamer forum
#1 : 03/03-22 15:44
Thomas
Thomas
Posts: 3
Hello world,

First of all i have to say that this is an amazing tool, thank you!

Second I am having a problem which i cannot figure out after a hours of searching and trying.. hope someone can help me out on this.

I am currently on parental leave and wanted to make a video of my firstborn's first 4 months while displaying her age in months & days (or weeks & days, whatever is easiest).
Luckily i found this tool which would save me quite some time! Unfortunately, the setup i am looking for is not in the presets. So i started to look around and see what other ways there were to work around this.

first the timestamp looked promising with the option for delta date and time, however i could not figure this out (tried to put in the DoB with a minus symbol, but it would give an error).
Secondly i tried messing around with 'New name' presets and tried to find a way to use the unixtimestamp and deduct the DoB's unix time stamp and convert it back to days.
This sounded doable in my mind until i started googling/searching for how to do this. I tried to comprehend this but i cannot figure javascript out so easily and make something out of it. After some more googling trying to modify a existing script i gave up because it was too much (my programming knowledge is from basic assignments at university (matlab & python).
Lastly i gave up and started looking on the forums if perhaps someone else had the same idea and there was a solution for this (should have been my first step, i know).

On the forums i came acros another user 'Zaibes' who posted his manual script to do exactly what i was looking for. I was very happy and quickly tried the script unfortunately without any results. The 'New Filename' column doesnt show any change and is greyed out. I thought this might be because of a small error in the script so i started to trace it and see if i can 'fix' this. Tried multiple (probably stupid) stuff, all without results.

So after half a day of trying to figure this out my last hope is the forums. Maybe someone can figure out how to achieve my goal? Maybe someone who is familiar with javascript can figure out why the script is not working for me? Thank you in advance!

Below is the script i copy pasted from 'Zaibes' post (dating 2020):
----------
function ageDifference(dateString, datePicture) {

var dob = new Date(dateString)
var datePic = new Date(datePicture);

if(datePic < dob) {
var tmp = datePic
datePic = dob
dob = tmp
}
var datePicYear = datePic.getYear();
var datePicMonth = datePic.getMonth();
var datePicDay = datePic.getDate();
var yearDob = dob.getYear();
var monthDob = dob.getMonth();
var dateDob = dob.getDate();
var age = {};
var ageString = "";
var yearString = "";
var monthString = "";
var dayString = "";

yearAge = datePicYear - yearDob;

if (datePicMonth >= monthDob)
var monthAge = datePicMonth - monthDob;
else {
yearAge--;
var monthAge = 12 + datePicMonth -monthDob;
}

if (datePicDay >= dateDob)
var dateAge = datePicDay - dateDob;
else {
monthAge--;
var dateAge = 31 + datePicDay - dateDob;
if (monthAge < 0) {
monthAge = 11;
yearAge--;
}
}

age = { years: yearAge, months: monthAge, days: dateAge };

if ( age.years > 1 ) yearString = " years";
else yearString = " year";
if ( age.months> 1 ) monthString = " months";
else monthString = " month";
if ( age.days > 1 ) dayString = " days";
else dayString = " day";

return age.years + yearString + " - " + age.months + monthString + " - " + age.days + dayString;

}

function isValidDate(dateString) {
// First check for the pattern
if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString))
return false;

// Parse the date parts to integers
var parts = dateString.split("/");
var day = parseInt(parts[1], 10);
var month = parseInt(parts[0], 10);
var year = parseInt(parts[2], 10);

// Check the ranges of month and year
if(year < 1000 || year > 3000 || month == 0 || month > 12)
return false;

var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

// Adjust for leap years
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
monthLength[1] = 29;

// Check the range of the day
return day > 0 && day <= monthLength[month - 1];
}

var dateTimeOriginal = ( item.exifToolValue('DateTimeOriginal') == '' ) ? item.exifToolValue('ModifyDate') : item.exifToolValue('DateTimeOriginal')
var y_original = dateTimeOriginal.substring(0,4)
var m_original = dateTimeOriginal.substring(5,7)
var d_original = dateTimeOriginal.substring(8,10)
var datePicture = m_original + '/' + d_original + '/' + y_original

var firstName = 'Nena'
var birthDate = '11/21/2021'
var ageString = isValidDate(datePicture) ? ageDifference(birthDate, datePicture) : 'Unknown Age'

return firstName + ' - ' + ageString + ' - ' + item.newBasename;



03/03-22 15:44
#2 : 04/03-22 18:54
David Lee
David Lee
Posts: 1125
The solution should be very much simpler - particularly if you will be happy with weeks and days!

Basically all you need to do is obtain date objects for both the date of birth and image dates and convert these into UNIX epochs - which are defined as the number of milliseconds since the beginning of time (which is defined as midnight 1/1/1970).

Then subtracting one from the other will give you the age in milliseconds, which you can easily convert into days or weeks or whatever, for use in the return string.

For example - if you wish to use the Date Modified timestamp then use the following three lines of code:

birthDate = new Date("2022-01-01").getTime();
fileDate = item.modifiedDate.getTime();
ageDays = Math.floor((fileDate - birthDate)/86400000);

new Date ("2022-01-01") will return the JavaScript date string corresponding to the date in quotes
.getTime() will convert the JavaScript date into a UNIX epoch
Finally divide by 86400000 (ms in a day) and round down to the nearest day.

If you wish to use EXIF data then the code will be very slightly more complicated, since the date format has to be converted to be compatible with a JavaScript date (we only need the date part and the "_" separators must be replaced with "-").

For example, using the "DateTimeOriginal" EXIF tag...

birthDate = new Date("2022-02-01").getTime();
exifDate = app.parseTags('<ExifTool:DateTimeOriginal>').slice(0,10).replace(/_/g,'-');
exifDate = new Date(exifDate).getTime();
ageDays = Math.floor((exifDate - birthDate)/86400000);

In either case the variable "birthDate" only needs to be set once, before looping through the files in the list, so you can put the first line in the "Pre batch script". In theory this should save some processing time but I expect it won't make much difference.


04/03-22 18:54 - edited 04/03-22 19:00
#3 : 04/03-22 19:07
David Lee
David Lee
Posts: 1125
Reply to #2:

In case you need a bit more assistance - to convert days to weeks & days:

weeks = Math.floor(days/7);
days = days % 7;

Obviously converting to calendar months will require much more complex processing - unless you would be satisfied with four-week lunar months.


04/03-22 19:07
#4 : 04/03-22 19:07
David Lee
David Lee
Posts: 1125



04/03-22 19:07 - edited 04/03-22 21:34
#5 : 05/03-22 17:24
Thomas
Thomas
Posts: 3
Reply to #3:
@David Lee,

Thank you very much! your response sounds promising and i will give it a go tomorrow and hope to figure it out. Will let you know if i do :), thanks again! Gives me hope to finish this project before having to get back to work :D.

And yes i would only need the days/weeks and 4 week month lunar cycle works good enough for me, at the beginning of life for baby's most books/educational material explains in weeks rather than months as it is more accuracte given the rapid pace of development.

Thanks and best regards,
Hopeful Dad


05/03-22 17:24
#8 : 10/03-22 09:54
Thomas
Thomas
Posts: 3
Reply to #3:
@David Lee
Took a bit longer than anticipated when i could work on this again, but finally have the time and it works!
Looking at the code this is exactly what i had in mind, just some simple arithmmetics on some values but couldnt figure it out myself.

Thanks again David for being so helpful and explaining it like that!

---------
Here is the final script i used if others want just to copy paste:

pre batch script:
birthDate = new Date("YYYY-MM-DD").getTime();
babyName = "insert_name"

fileDate = item.modifiedDate.getTime();
ageDays = Math.floor((fileDate - birthDate)/86400000);
weeks = Math.floor(ageDays/7);
days = ageDays % 7;
return babyName + '_' + weeks+'w'+days+'d' + '_' + item.newBasename;
---------
This gives me the easiest and best solution of both worlds --> i can just do the months myself by the original date and inserting a page/frame introducing the month (if required)

Thanks again!!!


10/03-22 09:54