In this post I want to spotlight the Format Date function, which is very useful and makes using time in shortcuts a breeze. I’ll explain how you can use it with a shortcut example.
What is the Format Date function? 🤔
The Format Date function formats date values as the name implies. However, it also gives you the ability to take a date value, extract the time value, and convert it into a number value (with the help of Get Numbers from Input). The real power sits in the ability to customize the date format.
(In case you’re using an adblocker, please consider whitelisting this site. Any revenue from Google ads goes to the costs of running this site.)
In the following examples we’ll tell time using military style time. What is military style time you might ask. It’s a way of telling time using the 24-hour clock instead of the 12-hour clock you might be used to. Time is given with 4 digits, where the two first digits represent hours, and the two following two digits represent minutes. Using it in HomeKit, the leading zeros will be taken away, as it will be converted into a simple number.A few examples:
12 Hour Clock
24 Hour Clock
1. Getting the current time, converting it into a number and saving it into a variable
If you want to use time as a condition in shortcuts, where different times of day will give you different results, this is the best way to do it. In this example we’re going to convert current time into a number (military style time), saved into a variable which then can be used in if-statements.
Start by adding the action Format Date. Select Date and you’ll be able to choose between Variables and Current Date. We want to choose Current Date.
Next, press Show More, Date Format and Custom. In Format String you’ll see the default custom Format String, a combination of upper- and lowercase letters. If you press the little Play button () in the bottom right corner, the shortcut will run in the current configuration and output the current date in the default custom format. It’s a very long format with weekday, date and month, time, and time zone information. This is not what we want.
Press within the default custom string format and replace the default value with the letters Hmm. This is case sensitive, so it’s important that the first letter is an uppercase H. The letter m, repeated twice, must be lowercase. Use no spaces, or anything else.
The H stands for hours in the 24-Hour Clock with no leading zero, and m stands for minutes. We put the letter m twice, because we want to get the number of minutes with two digits, meaning if the time was 08:05 AM, we want the minutes to be represented by 05, not just a single 5. If you press the little play button in the bottom right corner at this point, it will output the time with 3 or 4 digits (depending on the hours).
At this point, the shortcut interprets the number you’ve just extracted as a text value (a string value if you do programming), a text value that contains numbers. But we want to be able to compare different numbers to each other, and that’s why we need to convert it into a number value (an integer if you do programming). To achieve this, we use the action Get Numbers from Input.
It will preselect the value from the action above, in this case Formatted Date, exactly as we want. If you press the play button at this time, you will notice that it has removed any leading zeroes. If the time was 12:05 AM, it will now give you the number 5 (in military time 0005), which is exactly the way we want it to be.
At this time, we will save the number into a variable with the Set Variable action. Again, it will have prepopulated the input with the Numbers from the previous action. Select Variable Name and give it a descriptive name like TimeNow.
Naming variables properly is something very important in both programming as well as when writing slightly longer shortcuts. Giving variables names like a, b, c, x, y might seem ok at first, but when you need to remember which name represents what data further down in the shortcut, it quickly becomes difficult to work with. The same goes for when you go back to edit a shortcut you created weeks or months ago, you have no way of remembering what nondescript variable names represent.
2. Using the variable (number) in if-statements
At this point we can use the variable in an if-statement to control what the shortcut does depending on time of day. In this example we’ll turn the lights on with the brightness level 100% if the time is between 5:30 AM and 11:00 PM (530 – 2300) and turn them on with the brightness level 30% if it’s any other time (in this case 11:01 PM to 5:29 AM).
We do this by adding the if-statement, select TimeNow as Input, select is between as Condition, and add the values 530 and 2300 as numbers. Add a Control Home action directly below the if-statement, where the lights are turned on and set to 100% brightness. Add a second Control Home action and place it just below Otherwise. The second Control Home action turns on the lights to brightness level 30%.
3. Common mistakes when using this method
When using this logic in if-statements, it’s very important to remember that the if-statement only sees two numbers, and not time values. It’s easy to make the logical mistake of thinking that if we want the lights to be set to 20% between 10:00 PM and 3:00 AM, we can create an if-statement saying that if TimeNow is between 2200 and 300, turn the light on to 20% brightness. THIS WILL NOT WORK!
As the shortcut only sees the numbers as numbers, what the if-statement really says is that if the number in TimeNow is between 300 and 2200 (03:00 AM and 10:00 PM), do whatever comes next. When you compare a number to a range of numbers, you always start from the smaller number and go to the larger number in the range. If you need something to happen between sometime before midnight and sometime after midnight, you can do as we did in the example above, where we designed the logic in such a way that the problematic time range became the “Otherwise” statement. Another solution is to turn it into two if-statements:
If TimeNow is between 2200 and 2359
If TimeNow is between 0 and 300
In part two we’ll see how we can use the Format Date function using the same logic for sunrise and sunset, and in part three we’ll look at how to use offsets for sunset and sunrise (for example if TimeNow is between sunrise minus 30 minutes and sunset plus one hour).
If you’ve got any questions about what was explained in this post, please leave a comment and I’ll try to explain it more thoroughly.