Convert written date to numerical date

Advanced Renamer forum
#1 : 25/09-22 15:28
trainenthusiast
trainenthusiast
Posts: 3
Example file names

Jan 6 2020 - something - somethingelse.mp4
Apr 1X 2021 - something - somethingelse.mp4

I want to convert this to the following filenames respectively

2020-01-06 - something - somethingelse.mp4 (note the added 0 to the 6th of the month)
2021-04-1X - something - somethingelse.mp4 (note the X placeholder)

I thought the script method would be best applicable here. I have never used Javascript before so it's not good by any means. I first used another program and the following code to get the job done. Unfortunately I found out that that program requires a license to use Javascript renaming, hence why I use this program now.

----
function(index, item) {
function getMonthFromString(mon){
return new Date(Date.parse(mon +" 1, 2012")).getMonth()+1
}

function dateToYMD(date) {
var fullstring = date.split(' ');
var y = fullstring[2],
mfull = fullstring[0],
d = parseInt(fullstring[1]);
var m = getMonthFromString(mfull)
return '' + y + '-' + (m<=9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d);
}

function getThirdPart(str) {
return str.split('-')[2];
}

function getSecondPart(str) {
return str.split('-')[1];
}

function getFirstPart(str) {
return str.split('-')[0];
}

newdate = dateToYMD(getFirstPart(item.name));

newName = newdate + ' -' + getSecondPart(item.name) + '-' + getThirdPart(item.name);
return newName;
}
----
and hit Apply script. Unfortunately, the Filename and New Filename in the Rename Files window is the same., with Error saying "OK".

At first I thought this may be due to you having to return the full filename including extension, so I added + '.mp4', but still the same.

Then I thought it may be due to me using functions inside the function(index, item), so I tried the following

----
function(index, item) {
var firstPart = item.name.split('-')[0];
var secondPart = item.name.split('-')[1];
var thirdPart = item.name.split('-')[2];
var fullstring = firstPart.split(' ');
var y = fullstring[2],
mfull = fullstring[0],
d = parseInt(fullstring[1]);
var m = new Date(Date.parse(mfull +" 1, 2012")).getMonth()+1;
var test = '' + y + '-' + (m<=9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d);
var newName = test + ' -' + secondPart + '-' + thirdPart;
return newName;
}
----
which also didn't work (Filename and New Filename are the same), and I tried with + '.mp4', which also did not work.

What am I doing wrong?


25/09-22 15:28 - edited 26/09-22 14:17
#2 : 25/09-22 16:21
trainenthusiast
trainenthusiast
Posts: 3
Ignore this reply, this code does not work if the exact date is unknown (e.g. Apr 1x 2020)

I have another piece of code that does what I want in another program, but in Advanced Renamer I get the error "Invalid script: Invalid post script: uncaught: 'Invalid Date'", even if I replace firstPart with something like "Jan 9 2020".

----
firstPart = item.name.split('-')[0];
secondPart = item.name.split('-')[1];
thirdPart = item.name.split('-')[2];

var aDate = new Date(firstPart);

test = aDate.toISOString().slice(0,10);

newName = test + ' -' + secondPart + '-' + thirdPart;
return newName;
----


25/09-22 16:21 - edited 25/09-22 16:47
#3 : 28/09-22 14:06
David Lee
David Lee
Posts: 1125
Reply to #1:

You don't need to use scripting - just build a Method list with four Methods as follows...

1. Renumber method - zero-pad the month:
Number position: 1
Change to: Relative to existing number
Number difference: 0
Zero padding: manual
Number length: 2

2. Replace method - remove leading zero when the placeholder "X" is present:
Replace: ^[^\d]*\K0(?=\dX)
with: nothing
Use regular expressions

3. List replace method - convert month abbreviations to numbers
Add 12 lines:
Replace: Jan with: 01
Replace: Feb with: 02
etc

4. Replace method - reorder date:
Replace: ^([^ ]*) ([^ ]*) ([^ ]*)
with:\3-\2-\1
Use regular expressions

Note the space at the end of the regular expression.





28/09-22 14:06
#4 : 30/09-22 16:51
trainenthusiast
trainenthusiast
Posts: 3
Reply to #3:

Thank you, I was thinking to complicated. It worked great! Step 4 I replaced with

"\3-\1-\2 " to get the yyyy-mm-dd format



30/09-22 16:51