How do I convert a date from one format to another?

I want to prompt the user for a date and save it to two named clipboards in two different date formats. For example, on clipboardone the date should be 10/13/2014 and on clipboardtwo it should be October 13, 2014.
Thus far, I can prompt the user for input, place the value into a variable (dateone) and set a clipboardone to the variable.
How do I then convert dateone into the different format for clipboardtwo? I have tried using the first variable (dateone) as a token in the expression for setting clipboardtwo, but the syntax escapes me.

Here's one way to do it:

If you have questions on how it works, feel free to ask.

Thank you for the suggestion, Philippe.

I gave it a try, but the Python datetime.datetime.strftime() function keeps throwing errors because the input to the function does not conform to its expected format.

I’ll play with it more some other day.

Russ Cusimano

strftime expects a datetime object. So it’s probably the date_object assignment above that fails. In my example its input is supposed to be formatted as “yyyy/mm/dd”. If the string you pass it through the KM variable is formatted differently, you’ll have to update the pattern that describes that format (’%Y/%m/%d’ represents ‘yyyy/mm/dd’).

You can find all the format codes listed here:

Thank you. That’s very helpful.

As I was driving home this evening, I realized I had written a date formatting AppleScript a couple years ago. I resurrected that code and adapted it.

You sent me in the direction of using a script, and I am grateful for that.

Best regards,

Russ Cusimano

Scripts are great, but for this there is really no need. Keyboard Maestro can either prompt for the date components (Day, Month, Year), or can break apart a m/d/y formatted date, and then format it any way you like. Here is an example:

Thank you, Peter. That is an elegant solution.

The RegEx works as expected, but, when I reformat the variables with ICUDateTimeFor%TIME() in a Set Named Clipboard command, the date value is decreased by 1. For example, Dec 31, 2104 becomes Dec 30, 2014.

Seems rather odd. Any suggestions?

Russ Cusimano

Ahh, yes, I always forget that (so everyone else is doomed I suppose). TIME is in GMT. But the dat is in local time. I’m at +8:00 so it doesn’t affect me, but those of you in negative timezones get mucked around.

In the TIME function, instead of TIME(Year,Month,Day), use TIME(Year,Month,Day,12,0,0) - ie, 12 noon, GMT. At 12 noon GMT, pretty much everyone on earth is in the same day (sorry to the tiny fraction of folks who manage to be more than 12 hours off GMT).

Thanks. I thought it might be that or perhaps TIME requires a time argument.

Russ Cusimano