Reformatting user-entered date

Hi Smart People—

I'm trying to allow the user to input a date in numeric form and produce the same date in "word" form—so user enters "09/03/25" and macro produces "September 3, 2025." Here's what doesn't work:

  1. Use "Prompt for User Input" to get variable "InvoiceDate" as a Date
  2. Set variable DateString to %ICUDateTimeFor%InvoiceDate%MMMM d, yyyy%

That just gives me a "failed to evaluate" error. If I check the value of InvoiceDate in between steps 1 and 2 above, the value is 1756900800, which sure looks like a valid TIME value to me, but it seems likely that I'm misunderstanding the way that "InvoiceDate" variable is viewed by KM… right?

Thanks in advance for any shared wisdom!

David

For some time I have been using a Subroutine (with some AppleScript code written by the great @ccstone) which parses dates from user input to whatever format I want to use.

So, there is a Subroutine that does the work which is called from your Macro. Here is a video showing what it can do. I started with the example you gave but it can do any format to any other format including phrases like "tomorrow" or "next Wednesday" or "2nd March 2024" or "23/8/2023"

I use is as part of a Macro I have to save receipts converting the date on the PDF receipt to a standard format that I want to use.

Date Parsing

Here is the Subroutine and an example caller Macro. It will import disabled as a Macro Group but you can then move the subroutine to another Group of your choice.

Date Parsing Macros.kmmacros (14.1 KB)

The Action that calls the Subroutine looks like this:

EDIT: I realise on my Mac this is working with European Date Numeric Format (day/month/year) rather than US Date Format (month/day/year) but I assume that is just matching my Mac's date setting.

2 Likes

While @Zabobon's macro is the best answer in terms of date formatting flexibility, in terms of figuring out what's going wrong with your original approach, all I can offer is "seems to work here."

Here's my macro:

Format Date.kmmacros (3.4 KB)

When run:


You didn't post your macro, so I can't tell why it didn't work, but I suspect it's got something to do with the variable inside your %ICUDateTimeFor... token.

-rob.

2 Likes

@griffman, you make it look so easy... and I suppose it is, once you know how (I made an attempt that came very close but which erroneously used the TIME token and that’s where it all went wrong).

@peternlewis, I think this macro would serve as a very useful example in the Wiki.

I know I am not alone in finding that date formatting in KM can make one’s head spin somewhat, especially since the information is spread out between the Wiki and linked Forum discussions. The information is there but can take some piecing together and that’s when confusion can set in. #2c

Anyway, back to @griffman’s macro… Note that the year input is in the format YYYY whereas OP required YY. If that is truly what is intended for the range of dates required, YY could be converted to YYYY by adding 2000 to the year value (unless “00” is meant to represent the year 3000 rather than the year 2000).

However, since the input field in the prompt is set to “Date” (in the field’s dropdrown menu), the unix date that that provides would need to be converted first to isolate the year value. This would, I think, be less messy than applying a calculation directly to the Unix time value.

Aside from the question of YYYY versus YY, I presume, @anythingbutdave , that you have a particular reason to use the format MM/DD/YY rather than the international format YY/MM/DD, but if not, I would suggest the latter as the default always since, besides being the international format, it works best for filing to have the year value first.

1 Like

Sorry, I forgot to mention that bit—yea, it's a lot trickier if you have to support a two-digit input year.

As an alternative, if a two-year date input were required, you could just set the input field to text, not date, then parse the text to format the year into four-digit form, then convert that into Unix time, and finally massage into written form. Something like this:

Format Date (two-digit year input).kmmacros (4.9 KB)

When run:


The downside to this approach is that you should really sanitize the inputs to make sure you're getting a valid date before proceeding. And you may have a time zone issue as you have to start at mid day to get the right date value out.

Basically, unless there's an absolute requirement for it, using a date-formatted input field with a four-digit year is a heck of a lot easier :).

-rob.

2 Likes

Defiinitely.

Also, I am pleased to have been reminded of how useful it is that KM provides various format options for field inputs!

1 Like

Actually, the YY was a brain lapse. :man_facepalming:t2: I'm just using the in-house KM User Input prompt, so:


I have that variable set to "Date" type, which (I assume?) automagically does the thinking and yields what would be the result of TIME(YYYY,MM,DD)… yes?

And thanks millions for the AppleScript routine, @Zabobon! I'll take a deep breath and try to get that to work for my case this afternoon. :crossed_fingers:t2::crossed_fingers:t2:

So ... did you get it working? Did you compare my posted macro to yours to see where the problem might be?

-rob.

Very nearly!

I installed ccstone's subroutine and swapped in the "execute subroutine" action from the Caller Example for my failed attempt with ICUDateTimeFor. So I run the macro and enter the date in the dialog as I designed it:


…but that gives me this error, which a little exploration reveals is generated by the subroutine:

Oddly, if I enter the exact same date in this dialog, the subroutine (and the rest of the macro) proceeds just fine.

So I guess now my puzzlement is, what's the difference between my date entry field and the subroutine's date entry field? :thinking:

If you post your macro, we can help you figure it out. Without that, though, it's tough. (Select your macro, click the Share button, select the KM Forum as the destination, then cut the text that appears in the new topic box and paste into a reply in this thread.)

-rob.

1 Like

Y'all are so tremendously generous! Here's what I have:

Insert Invoice Link into Email Macro (v11.0.4)

Insert Invoice Link into Email.kmmacros (4.9 KB)

There’s nothing obviously wrong…

I’d be interested to know how you get on with this – your original Prompt, but with the date formatting done in the “Insert” action:

Insert Invoice Link into Email v2.kmmacros (4.4 KB)

Image