Date formating on the fly YYYY-MM-DD vs DD-MM-YYYY vs MM-DD-YYYY

Hello,

As most of you, I’m all the time fighting different date formats… Sometimes I copy a text and the date comes as 23/09/2023, but I need to use it as 2023-09-23, and vice versa. To make things a little bit more complicated, sometimes I get the date in the US format, like 09-23-2023.

I’ve been struggling with a way to automate the change in format. I want to create a keyboard shortcut that takes the date from the clipboard, and paste it in the format I need.

But I simply don’t know where to start.

Any Ideas of how to built this?

There are a large nunber of ways to tackle this using different definition logic, approaches, and tools.

Here is an example using Shortcuts illustrating just one of them.

https://www.icloud.com/shortcuts/62364f0ab04144f6aef44643a3b7019b

Hope that helps.

1 Like

My first thought was to break out into a shell script to parse the date. Use your scripting language of choice and a decent date parsing library to parse and return the date as a date object. However, every decent date parsing library I’ve seen (mostly Python and JavaScript for me) all require you to specify the format of the string to be parsed. Sure, within a format specification there is variability which the parser handles just fine, but anything outside of the spec will fail.

And the 3 formats you listed each fall under different specs. Well, actually, 2023-09-23 is ISO 8601, but the other two are ad-hoc formats which are not interchangeable. Sure, 23/09 and 09/23 are easily guessable by a human, but what about 05/09 and 09/05?

In the end I think probably the only way to make this work is to have a dialog which displays the date string and allows you to select from a list of common formats. In other words, a human needs to determine which format the existing string is.

Although is not the best solution yet, I found a quite easy way using Text Soap. With a regex find and replace. I called it Reverse date. It works both ways, using the same action.

The regex string I’m using is:
(\d{2,4})/-/-

and the replace value is:
$3-$2-$1

Works just fine.

Thanks Guys!!

By the way, I managed to make an apple shortcut out of it:

https://www.icloud.com/shortcuts/0eb1035c73b545258a1a5f5add3a8e97