Parsing date strings in Shortcuts
September 4, 2020 at 11:39 PM by Dr. Drang
I didn’t realize until today that Shortcuts has a date parser. It doesn’t call itself a date parser, and the documentation doesn’t explain what kinds of text strings it can parse, but it’s Shortcuts, so what would you expect?
I found it more or less by accident as I was writing a shortcut similar to this one from a few months ago. Last year, I switched from monthly contact lenses to semi-monthly. They’re thinner, more comfortable, and settle in place almost immediately.1 But without a big, obvious change in the calendar, I’ve found it harder to remember to change them in the middle of the month. On the plus side, I’ve inadvertently learned that I can wear them much longer than 15–16 days with no ill effects.
Based on this experience, I’ve decided I should switch them out every three weeks. Again, this is not a schedule I’m going to remember on my own, so I built a shortcut to run on the night I throw a set of lenses away. It adds an event to my personal calendar to remind me to throw the next set away in three weeks.
I expect to run the shortcut at any time of day, possibly even the next morning when I open up a new set of contacts. But I want the event—and the alert associated with it—to be at a particular time in the evening. This means I can’t get the date and time of the event by just adding three weeks to the date and time at which I run the shortcut. And I certainly wasn’t going to force myself to use those idiotic spinners to set the time—the time is fixed and the shortcut should handle it automatically.
Here’s my solution, which you can download:
Step | Action | Comment |
---|---|---|
1 | Get the date I threw away the last set of contacts. This should be the current date, but I allow an adjustment in case I run this the next morning. | |
2 | Create a text string with the date from Step 1 and a time of 9:00 pm. The format of Provided Input is shown later in the post. | |
3 | Here’s the parsing step. It turns the date string from Step 1 into a date/time combo. | |
4 | This is the date/time for the calendar event. The magic variable for this step is called End Date. | |
5 | If I throw the contacts away on the date set in Step 1, I start wearing a new set a day later. This magic variable is called Start Date. | |
6 | Here we create a new calendar event for throwing away the next set of contacts. There’s an alarm and some information in the Notes field. |
Here’s how the Provided Input date is formatted:
As I said at the top, I don’t know all the formats the yyyy-mm-dd HH:MM:SS
would work, and it does.
Most scripting languages I’ve used have a way to change some date/time fields while leaving the others untouched, but Shortcuts apparently doesn’t. I was searching and scrolling, trying to find such a command, when I ran across
. It does the job.Why don’t I just set up an event that repeats every three weeks? Mainly because of my poor experience with doing that for switching out my CPAP supplies: the real world often intervenes, forcing you to change your carefully planned schedule. I find it easier to run a shortcut than to reset a recurring event. If I find that my contact lens schedule doesn’t change after several months of using this, I’ll switch to a long-running recurring event.
Update Sep 12, 2020 7:27 AM An updated shortcut, with a different way of setting the time and a workaround for a Shortcuts bug, is here.
-
I have astigmatism, so my contacts aren’t axisymmetric; they have to be oriented a particular way to work. They’re weighted to help them spin around to the right angle. When I was wearing monthlies, the right contact would usually take at least five minutes to rotate to the correct position and sometimes wouldn’t settle in for half an hour or so. ↩