convert Unix TimeStamp in filename to normal date format

Advanced Renamer forum
#1 : 08/03-21 07:05
G
G
Posts: 4
Hi, I have these folders that I want to add the 'last modified' date to them. As using the Date/Time Modified Tags don't work on folders I tried the <UnixTimestamp Modified> tag and it does, conventing the string it generates shows the same date as in the folder properties! Now is there a way to convert this Unix "Epoch" TimeStamp to a normal date format like YYYYMMDD_HHMM?

Thanks.


08/03-21 07:05 - edited 08/03-21 07:07
#2 : 08/03-21 14:29
David Lee
David Lee
Posts: 662
Try this script:

epoch = app.parseTags("<UnixTimestamp Modified>");
dat = new Date(1000 * epoch);
return dat.getFullYear().toString()
+ ("0" + (dat.getMonth() + 1)).slice(-2)
+ ("0" + dat.getDate()).slice(-2)
+ "_" + ("0" + dat.getHours()).slice(-2)
+ ("0" + dat.getMinutes()).slice(-2);


08/03-21 14:29
#3 : 08/03-21 21:19
G
G
Posts: 4
Reply to #2:

:oThank you so much! It worked great

I just had to change the "timezone" adding three hours to the Date() and it printed the correct results.

here's the script I used just for reference:

Date.prototype.addHours = function(h){
this.setHours(this.getHours()+h);
return this;
//snippet from https://www.codexworld.com/how-to/add-hours-to-j avascript-date-object/
}

epoch = app.parseTags("<UnixTimestamp Modified>");
//.addHours to offset the timezone
dat = new Date(1000 * epoch).addHours(3);

//Keep original filename
return item.newBasename + " " +

//+ the converted date
dat.getFullYear().toString()
+ ("0" + (dat.getMonth() + 1)).slice(-2)
+ ("0" + dat.getDate()).slice(-2)
+ "_" + ("0" + dat.getHours()).slice(-2)
+ ("0" + dat.getMinutes()).slice(-2);

//Thanks to David Lee (https://www.advancedrenamer.com/forum_thread?for um_id=11608)

Again, thank you :)


08/03-21 21:19 - edited 08/03-21 21:21
#4 : 09/03-21 12:57
David Lee
David Lee
Posts: 662
Reply to #3:
The problem is that JavaScript is interpreting the epoch as UTC and then incorrectly applying a timezone correction to local time, which you then have to reverse. Just change all the dat.get methods to return date/times as UTC and everything should work without having to correct the timezone.

ie

epoch = app.parseTags("<UnixTimestamp Created>");
dat = new Date(1000 * epoch);
date = dat.getUTCFullYear().toString()
+ '-' + ("0" + (dat.getUTCMonth() + 1)).slice(-2)
+ '-' + ("0" + dat.getUTCDate()).slice(-2)
+ " " + ("0" + dat.getUTCHours()).slice(-2)
+ '-' + ("0" + dat.getUTCMinutes()).slice(-2)
+ '-' + ("0" + dat.getUTCSeconds()).slice(-2);
return(date);

The code for adding hours is unnecessary - all you have to do is add 3600 (seconds) to the epoch for each hour!

Note however that "new Date" requires the epoch in milliseconds - hence the multiplication by 1000


09/03-21 12:57
#5 : 10/03-21 10:34
G
G
Posts: 4
Reply to #4:
Thanks! I'm new to this script thing ^^


10/03-21 10:34
#6 : 10/03-21 13:29
David Lee
David Lee
Posts: 662
Reply to #5:

JavaScript is a bit of an uphill struggle - but with the right resources you should be able to get onto a steep learning curve. Google is your friend here - and I find the most useful general resources are at https://www.w3schools.com/js

You will also need to look into Regular Expressions (particularly in scripting). The introduction in the ARen User Guide is useful but it is limited to the basics and a great resource is https://www.regular-expressions.info

Best of luck!


10/03-21 13:29 - edited 10/03-21 13:31
#7 : 29/03-21 04:04
Tom Tarrant
Tom Tarrant
Posts: 1
Reply to #6:

David, I'm having a few similar issues and not very good at coding. I'm trying to rename many audio-files from audiomoth sensors. They are all UTC Time-stamped but I need to add 10 hours to make them reflect my local (Brisbane) time-zone.
So 20210327_150230.WAV should now read 20210328_010230.WAV

Would you be able to do write a script for Advanced Renamer to achieve this? (I have hundreds of files like this)
Any assistance much appreciated!

Tom


29/03-21 04:04
#8 : 29/03-21 10:12
David Lee
David Lee
Posts: 662
Reply to #7:

Tom,

Quite straightforward - parse the date string into a UNIX epoch, add 10 hours (in milliseconds) and convert back to the original format:

dateCorrection = 10*3600*1000;
D = item.name.match(/^(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})$/);
oldDate = Date.parse(D[1] + '-' + D[2] + '-' + D[3] + 'T' + D[4] + ':' + D[5] + ':' + D[6]);
newDate = new Date(oldDate + dateCorrection);
return newDate.getFullYear()
+ ('0' + (newDate.getMonth() + 1)).slice(-2)
+ ('0' + newDate.getDate()).slice(-2)
+ '_' + ('0' + newDate.getHours()).slice(-2)
+ ('0' + newDate.getMinutes()).slice(-2)
+ ('0' + newDate.getSeconds()).slice(-2);

You can make this more general to correct an arbitrary time error by setting dateCorrection in the Pre batch script - eg:

//Old Start Date "yyyy-mm-dd"
oldDate = "1970-01-01"
//Old Start Time "hh:mm:ss"'
oldTime = "00:00:31"
//Corrected Start Date "yyyy-mm-dd"
newDate = "2020-06-25"
//Corrected Start Time "hh:mm:ss"'
newTime = "21:30:15"
dateCorrection = Date.parse(newDate + 'T' + newTime) - Date.parse(oldDate + 'T' + oldTime);

I'm sure that you are aware that recent versions of the AudioMoth config app allow you to set filenames as local times.

You may find the "Bat Call Sound Analysis Workshop" Facebook group to be a useful resource. Whilst it was originally a UK-based group it now has a wide international membership and there is regular AudioMoth discussion.

David




29/03-21 10:12 - edited 29/03-21 10:13