Adjust Photo Time in File Name

Advanced Renamer forum
#1 : 17/06-21 20:39
Ryan W
Posts: 3
I have a bunch of photos that are named in the following way:
2021-06-1413_21_58.050377

Where the format is:
YYYY-mm-ddHH_MM_SS.SSSSSS

The issue I have is that the files have incorrectly been coded with the wrong time, and I need to reduce the hours (in 24hr format) by 5 hours. This would mean that the final file name should be:
2021-06-1408_21_58.050377

I found and adjusted some JavaScript that I think should work:
if (/^\d{4}-\d\d-\d{4}_\d\d_\d\d\.\d+/.test(name)) {
x = new Date(Date.parse(name.replace(/^(\d{4})-(\d\d)-(\d\d)(\d\d).*/, '$1,$2,$3,$4')));
function decrease() {x.setHours(x.getHours()-5); return x};
y = decrease(x);
month = ('0'+(y.getMonth()+1)).replace(/^0(\d\d)$/,'$1');
day = ('0'+(y.getDate())).replace(/^0(\d\d)$/,'$1');
hr = ('0'+(y.getHours())).replace(/^0(\d\d)$/,'$1');
newName = y.getFullYear()+'-'+month+'-'+day+hr+name.substr(12) }

However, it shows an error:
Invalid script: Invalid post script: uncaught: 'identifier \x27name\x27 undefined'

Please let me know how to resolve this error. Thanks!
#2 : 18/06-21 02:11
David Lee
Posts: 1125
You have several issues here:

The original filename is presented as item.name.

You must either return the value of newName (the normal method) or else set the value of item.newName.

The UNIX epoch should be in the format yyyy-mm-ddThh:mm:ss

You will need to deal with fractional seconds separately.

Try the following script:

match = item.name.match(/^(\d{4}-\d{2}-\d{2})(\d{2})_(\d{2})_(\d{2})(\.\d+)$/);
if (match[0]) {
epoch = Date.parse(match[1] + 'T' + match[2]+ ':' + match[3] + ':' + match[4]);
date = new Date(epoch - 5 * 3600000);
return date.getUTCFullYear()
+ "-" + ("0" + (date.getUTCMonth() + 1)).slice(-2)
+ "-" + ("0" + date.getUTCDate()).slice(-2)
+ ("0" + date.getUTCHours()).slice(-2)
+ "_" + ("0" + date.getUTCMinutes()).slice(-2)
+ "_" + ("0" + date.getUTCSeconds()).slice(-2)
+ match[5];
}

You should check recent forum threads before asking questions - if you had then you would have seen that I answered an almost identical question on 9th June, in the thread "Add or substract X hours from filename".