Changing Date formats

I have seen a number of posts regarding this subject. But I have a specific need.

I find myself having to rename files that already have a date in their filename. It would be lovely to have a menu of sorts that presents some common search and replace choices such as: MMDDYYYY to YYYY.MM.DD; MM-DD-YYYY to MM.DD.YYYY; MM/DD/YYYY to YYYY-MM-DD, etc.

I confess I tried using ChatGPT to accomplish this and it was a dismal failure. It seemed that this AI tool was just not familiar with Keyboard Maestro. Either that or I was not communicating properly. It kept telling me to click on things that just weren't there, and so on. I did, however, learn some things about regex syntax, which was instructive. Incidentally, I wonder which would be better to use in solving this: regex or string functions. I suppose they each have their pros and cons.

And I'm sure that with the help of the superior human minds out there in KM land (superior to ChatGPT, for certain) an elegant solution can be crafted. The ideal, once again, would function somewhat like this: I highlight a date that is in a format that I don't like. I activate the macro, and it converts the highlighted date to my preferred format. It is perhaps too much to ask (and probably more detailed than I want to get into this) that the macro actually recognize the offending format. I would be happy with just a menu offering different options that my own quantum brain could choose from.

I am not a programmer, but I like to use macros A LOT. And usually I can create a solution for myself. This one, though, has me stumped to the point that I have reached out to all you folks who have always been so very generous with your time.

As always, many thanks ahead of time.

I think slashes ("/") are prohibited in filenames. So that example won't be possible.

I'm a little puzzled about how to deal with international issues like the difference between DD-MM-YYYY and MM-DD-YYYY. how would a macro know if "12" is a day or a month? (Eg, is 12-11 the twelfth of November or the 11th of December?) This is also a problem because you said, "the macro should actually recognize the offending format." How does the macro determine if four digits are DDMM or MMDD? In fact, how would you tell if you saw something like 12-11-2001?

How exactly do you do "highlight"? Are you in the Finder app? Did you click on a filename in that app?

Actually, I can think of a solution that may be only a couple of actions in length. In fact, I think I can get it down to one action, although it would probably be 30% clearer if I did it in a handful of actions.

I'm gone for about 9 hours. Maybe while I am gone you can clarify some of my confusions.

One more question: Are you sure you want to do this one file at a time, instead of a whole folder at a time? Actually, I'm not even sure KM is the right tool for this. I think it can be done easily enough with a macOS command. Why do you want KM to do it, especially if you are asking for KM to do it one file at a time, when macOS can do many at a time?

It's true slashes are prohibited in filenames, but I thought I might use this elsewhere as well, like in a word document.

Sorry about the confusion regarding international issues. I don't really need the macro to "recognize" the date format. I can do that myself and make choices if there are choices to make.

To clarify further, I scan a lot of documents using ScanSnap Home. This scanning software names a scanned file itself somewhat arbitrarily. Then I have to change the filename to suit me before I save it to a folder. But I would like a macro that would work elsewhere besides just in ScanSnap. I also have a Stream Deck in which I have been incorporating macros. Just looking for a way to quickly change date formats on the fly.

Yes I highlight a date. But I should make that clearer. In the Finder I select a file, then I hit ENTER to change the filename and select the "date" part of the filename with the cursor. In a filename I sometimes select ONLY the date part of the filename and then type in my preferred format (or for that matter, any change I want to make).

Yes I am aware of how to do all this renaming in large groups of files that have a similar format using the RECORD QUICK MACRO action. were you thinking of a different way?

Would love to know how this is done in MacOS also.

Again, many thanks.

P.S. By the way, how do you do that cool thing where you quote part of my original post?

You select the text in the post that you want to quote, and press "Quote" which will appear once you select the text. This even works when you are composing a reply.

P.S. I'm still away for a few hours.

Okay, let's start with a simple piece of code. This will reverse the month and date if your selected string is in the format of DDMMYYYY or MMDDYYYY. That's currently the only transformation it does. I didn't want to facilitate more formats until I knew that you like this solution.

Of course, if you like this approach, it's probably not too hard for you to copy it for slightly different date patterns. You could assign different hotkeys to different date transformations.

Date transform aabbYYYY to bbaaYYYY Macro (v11.0.3)

Date transform aabbYYYY to bbaaYYYY.kmmacros (4.6 KB)

This works in the apps I tested, including Finder, but I can't guarantee it works in all apps. In order for this macro to work, you have to select text with 8 digits.

Let me ponder that.

There are several ways to do it with macOS.

But before I mention any of them, let me add that there is a popular command called "rename" that you could obtain via a popular utility called "homebrew". It's really designed to solve your requirement very easily. Are you willing to consider that option, or do you prefer a truly native macOS solution? Here is what it would look like:

rename -n 's/(\d{4})(\d{2})(\d{2})/$2$3$1/' *

It looks to me like this would be a superb solution for renaming all the files in a folder to match your preferred date pattern. Some of the other macOS solutions wouldn't be this short and simple.

Except they don't -- they have a series of characters that you interpret as a date. Compare that to "proper" dates and times in Excel or similar where you can easily change the formatting, where the underlying data is a date object, seconds since epoch, or similar where days, months and years have meaning.

So you are going have to parse the selected text, determine the part that is a date, then re-write that part to your preferred format. You are also going to have to make some assumptions -- for example, is "01-02-2025" the 2nd of January or, as in most right-thinking places :wink: , the 1st of February...

Since you are going to select the "date" part of the string yourself, that makes things easier. What you can then do is search for the various patterns, rewriting the one that matches into your preferred format. Something like this, long-winded but the logic should be clear once you know about "Try/Catch" actions:

Convert Text Dates.kmmacros (6.4 KB)

Image

And here's a "Switch/Case" version of the above, showing a few different ways of doing the replacement:

Convert Text Dates (Switch version).kmmacros (5.3 KB)

Image

Probably clearer/more understandable than the "Try/Catch" version but hey, I was doing something else try-catchy at the time...