Week Number Script not working

Advanced Renamer forum
#1 : 10/04-22 13:30
Matthias
Matthias
Posts: 1
Hello,

I have a script here that works without problems in plain JS (run and see console window in https://jsfiddle.net/q9f7m6yg/), but it does nothing in AFR. Since I cannot figure out what goes wrong, I thought I'd ask here. Basically, it replaces a timestamp 2022-03-22 with the year and the week number: 2022-12 (week 12 this year). I assume I made a trivial mistake…

Many thanks!

var dateStr = item.newName.substring(0, 11);
var year = dateStr.substring(0, 4);
var rest = item.newName.substring(11);
var tmpDate = new Date(dateStr);
var d = new Date(Date.UTC(tmpDate.getFullYear(), tmpDate.getMonth(), tmpDate.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
var wNo = Math.ceil(((d - yearStart) / 86400000 + 1) / 7);
return year + '-' + (wNo < 10 ? '0' : '') + wNo + ' ' + rest;


10/04-22 13:30
#2 : 11/04-22 11:25
David Lee
David Lee
Posts: 1125
The date "2022-03-22" contains only 10 characters. Replace the first line with:
var dateStr = item.newName.substring(0, 10);
and the script will work.

Alternatively, rather than re-inventing the wheel, you could have used the functions from:
https://weeknumber.com/how-to/javascript

Paste the code into the pre-batch script and then use:

dateStr = item.newName.substring(0, 10);
rest = item.newName.substring(11);
date = new Date(dateStr);
return date.getWeek() + "-" + date.getWeekYear() + " " + rest;




11/04-22 11:25