Workflow (iOS): Some Example Workflows for Dates

Whilst experimenting and answering occasional Twitter questions relating to date manipulation in the iOS Workflow app I’ve created a few example workflows. These workflows are not intended to be stand alone workflows with a practical purpose, but simply as ways that the dates can be manipulated.

Date manipulation is a common requirement. You may need to extract elements of a date (e.g. the month) or rearrange to another format. You can use them for queries, web site links, file names and a myriad of other things. So getting an idea of how to do some manipulations in Workflow could potentially prove quite useful.

The first example workflow we’ll look at is for converting a date in the format dd/mm/yyyy to a date stamp format of yyyy-mm-dd.

Workflow: dd/mm/yyyy -> yyyy-mm-dd

With this workflow we’re going to use the fact that we have a forward slash as a separator between the elements of the date to separate out the individual elements. We can do this using the list functionality in Workflow. Then we can store the elements in separate variables and recombine them in the format we want at the end.

The Workflow begins by getting a date input as a piece of text. The ‘Split Text’. 0 action then uses a custom separator of forward slash to create a list which is stored in the variable dtList.

The first item from the list is the day and this is assigned to the variable intDay.

The second item of the list we need to select by its place in the list (known as its index). So we again use the ‘Get Item from List’ action but this time specify the index rather than using the ‘First Item’ option. This second item corresponds to the month and is assigned to the variable intMonth.

The third item in the list is of course the year and this is assigned via the option to select ‘Last Item’ of ‘Get Item from List’ to the variable intYear.

At this point all of the date elements are held in separate variables and you could format or arrange these however you wish. In this workflow though we’re reversing the order and changing the separator to a hyphen so we can do this with a simple recombination in a Text action; viewing the result in a final ‘Quick Look’ action.

So there are some limitations here in that it really will expect the date to be in the correct format and if there’s a leading zero missing (e.g. 14/1/2014 rather than 14/01/2014) then it will look the same in the final answer. However we’ll take a look at something more around this shortly.

Workflow: Current Date Stamp (yyyy-mm-dd)

A particularly common requirement is to get the current date in the format yyyy-mm-dd. So not entered by the user (as above), but from the device. Fortunately there’s an easy way to do this.

The ‘Current Date’ action returns a full time stamp in the format “yyyy-mm-dd hh:mm:ss +####” - e.g. 2015-01-14 20:31:45 +0000. Notice that the start of the full time stamp is actually the date stamp and that’s what we want. So all we need to do is take the first ten characters in the time stamp and we’re done.

That’s actually not so easy to do in Workflow at the moment, so instead we’ll take a different approach. Rather than taking the first ten characters, we’ll discard the last fourteen instead. We can do this by replacing those fourteen characters with nothing.

To do this we’ll use a special feature of the ‘Replace Text’ action - the ‘Regular Expression’ option. A regular expression is a special notation that allows you to match patterns of characters rather than specific characters. In this case the fifteen characters at the end of the text we’re passing in.

If you take a look at the Workflow below, you’ll see the find text is set as “.{15}$” … which looks like gibberish at first. But let me explain. The first character in the find text is a period (“.”). This represents any character. Next comes a fifteen in between some curly braces (“{15}”). This indicates that we want fifteen of any character. Finally the dollar symbol (“$”) at the end of the line says that these any fourteen characters must come at the end of a line. Since we only have one line of text there’s only one place to find and replace this text; and that leaves us with our date stamp for the current date.

If you actually want the current date in dd/mm/yyyy format for example you could use the actions of the first workflow but in reverse. Split the date stamp using hyphens rather than forward slashes, capture the first item as the year, the second as the month, the third as the day and then recombine them from variables using forward slashes to separate them. But that’s quite frankly a lot of effort for something these regular expressions can help us with.

Workflow: Current Date (dd.mm.yyyy)

So as you might be able to tell regular expressions are a key aspect to dealing with dates currently in Workflow. You don’t have to be a regular expression guru to use them (I’m certainly not), but there this example shows how we can use a principle of pattern based substitutions to help us out.

We know that the ‘Current Date’ action is going to give us the current time stamp and the first ten characters give us the date stamp. From this we also know that characters 1-4 give us the year, 6-7 give us the month and 9-10 give us the day. There is a ‘Format Date’ action built into Workflow that can automatically format this time stamp into dd/mm/yyyy (as short date format - at least it is with my UK regional settings), but here we’re looking to use periods as the separators instead.

Let’s look at breaking the time stamp text into groups.

Character 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Format y y y y - m m - d d   h h : m m : s s   + # # # #
Group G1 G2 G3 G4 G5 G6

We’re actually only interested in groups G1, G3 and G5. G2, G4 and G6 we could discard without losing any information required for creating out date in the desired format. Fortunately regular expressions provides us with a way to specify groups using parentheses/brackets (“(“ and “)”).

to define a group we can put it in brackets and to reference a group in a replace we use the number of the group (relative position) preceded by a dollar symbol (“$”). In our case we need to specify each of the groups in the find text of the ‘Replace Text’ action and then build up our date as “$5.%3.$1”.

Workflow: Date (?d/?m/yyyy) to Date Stamp (yyyy-mm-dd)

So for the last workflow example I’m going to take a look at converting a date in a mixed length format to a date stamp. By a mixed length format I mean a date which may or may not include zeros on the day and month. Perhaps even a mixture of the two.

As you might have guessed we’re going to get some use again out of ‘Replace Text’ and regular expressions. But in this workflow the first thing I’ve done is provide a list of some dates in different lengths and structures (but all of the form ?d/?m/yyyy) to select from.

Once you select a date this is passed through to the first of three ‘Replace Text’ actions. It should look quite similar to some of the previous regular expression find texts except here you might notice that there are some slashes and the groups have a bit of a different structure.

First of all the forward slashes are literally just forward slashes. Rather than specify an unknown separator we’re going to go with the fact on this occasion that we know forward slashes are being used as the separator. So we can just go ahead and mix those straight in with the regular expression pattern groups.

Replacing the periods (which if you recall represented any character), we’re using square brackets (“[” and “]”) to specify numeric digits between 0 and 9 inclusive. Rather than specifying a single number of occurrences, two of the pattern in curly braces are specified as {1,2}. This simply means it will match against 1 or 2 characters.

i.e. “/[0-9]{1,2}/” would match “/7/” or “/07/”.

We can then use each group to rearrange the order irrespective of whether a month or day element of the date is specified as one or two digits.

The subsequent two ‘Replace Text’ actions then take this approach and use it to add in any missing zeros. The first does this by searching for a single digit month (with a one or two digit day) and prefixes a zero to the month if it finds a match. The second does the same thing but for the day.

There are any number of ways you can manipulate dates or in fact any structured text. In fact you should now be able to return to the earlier examples and tweak them to match the patterns or reformat the dates in different ways. Whatever you do I hope that you found some insight into using Workflow and that you can now make that awesome workflow you were dying to put together.

If you want to learn more about regular expressions then there are a huge number of resources online.

If you found this post useful why not share it with your Workflow using followers on you favourite social network(s).

Author: Stephen Millard
Tags: | workflow | ios |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read