Replace single digit date number with double digit (1 to 01)

I'm trying to prompt a user for a date (dd/mm/yy) and convert that to %ICUDateTime%yyyyMMdd%, so 26/07/20 to 20200726. I can do most of that with the macro below, but if the user types 26/7/20, instead of '07' for month, I don't get the result I'm looking for. I've never really fully been able to understand date manipulation with ICU dates and KM. Is there a simple way to do this. Threads such as https://forum.keyboardmaestro.com/t/convert-date-string-from-one-format-to-another-example/10230, although wonderful, seem over the top for this job.

Keyboard Maestro Actions.kmactions (1.6 KB)

Why not get them to put the date in three separate fields to start with
then you can validate the month more easily?

Thanks Jonathon, good idea, but I wonder if it's necessary? And couldn't they still fill out '7' for month instead of what I need which is '07'? But you have made me think of another solution, which probably isn't the best but should do the job. A Regex search on the day and month to see if it's one digit only, and if so, add a '0' before it. I just wish I could understand dates a little better, as I think there must be an easier way.

Some of my colleagues will write the most complex regex scripts to achieve the impossible, my take is that you are using Keyboard Maestro and separate input fields simplifies the Regex immensely.

see

you can also default the year more easily so it really is only two fields of input.

I'm not sure I fully understand, but I think your suggesting that I keep it simple? I think you're probably right, I don't know why I have an aversion to multiple fields for a date in this case, but parsing the date into 3 fields is not my issue in this case. I do get your point though.

I think however I've solved the issue with Regex in the following image. In fact, the 'If, then' action is completely unnecessary if I turn off 'Abort and Notify on Failure' like you suggest in your useful macro.

Thanks @jonathonl for the helpful feedback.

*Edit: The first one of these 'If' statements should return the changed variable to the source, not the clipboard of course, but that's not too important out of context.

The solution is simple -- you don't need to worry about the leading zero.
Just use this one Action:


%ICUDateTimeFor%TIME(2000+Rideyear, Ridemonth, Rideday, 12,0,0)%yyyyMMdd%

Example Output

image

1 Like

I love it. That formula actually clears a lot up for me. I could get (to a degree) ICUDateTime to work for todays date, but I didn't understand for to use it for a user inputted time.
Many thanks.

1 Like

Hey I had the Same problem Just Add another d after the d

1 Like