24-hour format to 12-hour format + am/pm

Advanced Renamer forum
#1 : 09/10-16 04:29
menace97
menace97
Posts: 9
I was looking to find a way to rename some family genealogy files that use a 12-hour format instead of the much more logical 24-hour naming convention.

I noticed that after searching around, it looks like this might be difficult without knowledge of how to use javascript, is that right Kim?

I see you don't like 12-hour formats, and don't plan on implementing any such method in the near future?

See link for clarification:
https://www.advancedrenamer.com/forum_thread?for um_id=1726

While I completely agree that 24-hour logically makes a lot more sense for sorting/archiving computer files which are named in a logical manner, unfortunately many parts of the world still use 12-hour format time - and follow through with this in their computing habits.

That was a while ago you made that statement Kim, and I was just curious if there is any change in your opinion if you might add that functionality later on, or even if you know of an easy way to get this to work with AdvancedRenamer?

e.g.
a file named:
lake 20-56-30.png

would like it to be renamed to:
lake 08-56-30 pm.png

There are about 500-600 files this family member wishes to rename, so I thought it might be worth a shot to ask this great community if there might be an easy way to do so for someone who doesn't know anything about Javascript. :)

Thanks!


09/10-16 04:29
#2 : 10/10-16 20:06
Tester123
Tester123
Posts: 92
Reply to #1:
Try this script:

--- Start of Script ---
var fileName = item.newBasename;
var newFileName;

// The time must be in HH-MI-SS (24-hour) format with leading zeros
// to pad each element to 2 digits, and must be at end of filename.
var regexTime = /(?:[01]\d|2[0-3])-[0-5]\d-[0-5]\d$/
var timeSection;
var hour;
var am_pm;

// Extract the time from the filename
timeSection = fileName.match(regexTime);

// If time not found, keep original filename
if (timeSection == null)
newFileName = fileName;
// If time is found
else {
timeSection = timeSection.toString();

// Extract the hour element and convert the string to a number
hour = timeSection.substring(0, 2);
hour = parseInt(hour, 10);

// Determine if AM or PM
if (hour >= 12 && hour <= 23) {
am_pm = "pm";
if (hour >= 13)
hour -= 12;
} else
am_pm = "am";

// Construct the new filename in these chunks:
// 1) The original filename except last 8 characters (i.e. the time)
// 2) The derived hour (converted to 12-hour format)
// 3) The original minutes and seconds
// 4) The derived AM/PM indicator
newFileName = fileName.substring(0, fileName.length - 8)
+ ( (hour < 10) ? "0" : "" ) + hour
+ timeSection.substring(2, 8)
+ " " + am_pm;
}

return newFileName;
--- End of Script ---


10/10-16 20:06
#3 : 20/10-16 09:45
menace97
menace97
Posts: 9
Reply to #2:

Thank you both very much! I tried adding both scripts using my test file named:
"lake 20-56-30.png"

and Rynos, your script worked perfectly with it coming up as:

"lake 08-56-30 PM.png"

but for some reason tester123, I received an error when trying to rename the file. I'm not very skilled at using Advanced Renamer at all, so I'm not really sure why or where there was a hiccup in the renaming process.

Thanks for all your help. Cheers!


20/10-16 09:45
#4 : 20/10-16 18:09
G. Lambany
G. Lambany
Posts: 187
Reply to #3:

here is the code menace is talking about:

http://pastebin.com/5aFPjkCv

reason tester123 code doesn't work with your example is because the problem was presented with something like that "20-56-30.png", no text before or after the date

I just made my code handle these kind of situation in advance

cheers


20/10-16 18:09
#5 : 20/10-16 19:51
Tester123
Tester123
Posts: 92
Reply to #4:
Thanks G. Lambany, should have thought about files not conforming to the pattern!


20/10-16 19:51
#6 : 21/10-16 00:06
G. Lambany
G. Lambany
Posts: 187
Reply to #5:
Your regex is much better tho, best thing would be to combine both.. : )


21/10-16 00:06
#7 : 22/10-16 00:22
menace97
menace97
Posts: 9
Reply to #6:

Sincerely, thank you both very much!


22/10-16 00:22