From unix timestamp to date time
Goal: Rename the original file name from UNIX timestamp to its corresponding date and time
Target example:
File name: 1779886134 (a string of correct UNIX timestamp digits)
Rename: 2026-5-27 20:48:54
Because when I was organizing photos, I found that many of the file names are UNIX timestamps. In order to facilitate intuitive date and time lookup, I want to rename them all to the date and time corresponding to the timestamp
Thank you for reading. I would greatly appreciate it if you could help me
Target example:
File name: 1779886134 (a string of correct UNIX timestamp digits)
Rename: 2026-5-27 20:48:54
Because when I was organizing photos, I found that many of the file names are UNIX timestamps. In order to facilitate intuitive date and time lookup, I want to rename them all to the date and time corresponding to the timestamp
Thank you for reading. I would greatly appreciate it if you could help me
Reply to #1:
Welcome Bob!
1) Create a script method. (See https://www.advancedrenamer.com/user_guide/v4/method_script )
2) Paste this next code in the "function(index, item)" block. Be sure to press the "Apply script" button:
//-------------------------------------------------------
//-- Extract any Unix timestamp (10 or 13 integer digits)
const timeStampRegex = /\b(\d{10}|\d{13})\b/;
let nameParts = item.newBasename.split (timeStampRegex);
//-- Reformat any time parts using local machine's timezone
let newNameParts = nameParts.map (piece => {
if (timeStampRegex.test (piece) ) {
let numTimeStamp = parseInt (piece, 10);
if (piece.length == 10) {
numTimeStamp *= 1000; // Ensure proper epoch time.
}
let dateObj = new Date (numTimeStamp);
return app.formatDate (dateObj, "yyyy-mm-dd hh_nn_ss") // colon not allowed
}
else {
return piece;
}
} );
return newNameParts.join (""); // set the new name
//-------------------------------------------------------
This will reformat the dates as you wished. But note that colons (:) are not allowed in filenames.
Regards,
Randy
Welcome Bob!
1) Create a script method. (See https://www.advancedrenamer.com/user_guide/v4/method_script )
2) Paste this next code in the "function(index, item)" block. Be sure to press the "Apply script" button:
//-------------------------------------------------------
//-- Extract any Unix timestamp (10 or 13 integer digits)
const timeStampRegex = /\b(\d{10}|\d{13})\b/;
let nameParts = item.newBasename.split (timeStampRegex);
//-- Reformat any time parts using local machine's timezone
let newNameParts = nameParts.map (piece => {
if (timeStampRegex.test (piece) ) {
let numTimeStamp = parseInt (piece, 10);
if (piece.length == 10) {
numTimeStamp *= 1000; // Ensure proper epoch time.
}
let dateObj = new Date (numTimeStamp);
return app.formatDate (dateObj, "yyyy-mm-dd hh_nn_ss") // colon not allowed
}
else {
return piece;
}
} );
return newNameParts.join (""); // set the new name
//-------------------------------------------------------
This will reformat the dates as you wished. But note that colons (:) are not allowed in filenames.
Regards,
Randy
Reply to #2:
Hi guys,
That's a fine piece of code there, Randy my friend. It would have taken me twice the lines, most likely, js duffer that I am.
I don't mean to be a noodge, or kvetsch, or even kibitz, :) well maybe kibitz (and I haven't actually *tried* the code, so I could be mistaken entirely), but shouldn't that line
const timeStampRegex = /\b(\d{10}|\d{13})\b/;
be reversed? i.e., const timeStampRegex = /\b(\d{13}|\d{10})\b/;
due to the way regex engines work? As I understand it, if it finds 10 digits it will stop, never checking for the 13 digits on the rights side of the regex alternation. It wouldn't effect creating a human-readable timestamp, but if there were 13 digits it seems it would never check the last 3 digits.
Again, I could be wrong, but that's how my grampappy taught me growin' up on the farm. Thoughts?
Best,
DF
Hi guys,
That's a fine piece of code there, Randy my friend. It would have taken me twice the lines, most likely, js duffer that I am.
I don't mean to be a noodge, or kvetsch, or even kibitz, :) well maybe kibitz (and I haven't actually *tried* the code, so I could be mistaken entirely), but shouldn't that line
const timeStampRegex = /\b(\d{10}|\d{13})\b/;
be reversed? i.e., const timeStampRegex = /\b(\d{13}|\d{10})\b/;
due to the way regex engines work? As I understand it, if it finds 10 digits it will stop, never checking for the 13 digits on the rights side of the regex alternation. It wouldn't effect creating a human-readable timestamp, but if there were 13 digits it seems it would never check the last 3 digits.
Again, I could be wrong, but that's how my grampappy taught me growin' up on the farm. Thoughts?
Best,
DF
Reply to #3:
Hi DF,
You would be correct IF I had not used \b to bracket the capture. That is crucial. Otherwise things like "22461270409473815009" would (time)bomb regardless.
Regards,
Randy
Hi DF,
You would be correct IF I had not used \b to bracket the capture. That is crucial. Otherwise things like "22461270409473815009" would (time)bomb regardless.
Regards,
Randy
Reply to #4:
Ah, correct sir! :) Now let's see if I can make this look like I knew that and was just testing you...
NOPE! :)
Best, and thanks for the lesson,
\bDF\b
Ah, correct sir! :) Now let's see if I can make this look like I knew that and was just testing you...
NOPE! :)
Best, and thanks for the lesson,
\bDF\b
Reply to #2:
I want to tell you that I am extremely grateful for your help!
When I was using AR, I was very certain that it could achieve this function, but I don't have programming skills, so I have been troubled by this function for a long time.
Following your guidance, I tried to fully achieve the effect I wanted to achieve, which saved me a lot of time and energy.
Thank you again for your help!
I want to tell you that I am extremely grateful for your help!
When I was using AR, I was very certain that it could achieve this function, but I don't have programming skills, so I have been troubled by this function for a long time.
Following your guidance, I tried to fully achieve the effect I wanted to achieve, which saved me a lot of time and energy.
Thank you again for your help!
Reply to #6:
You're very welcome!
It brightens my day when I can help.
You're very welcome!
It brightens my day when I can help.