The power of the Format Date function and how to use it for shortcuts using time of day or sunrise/sunset (part 1)

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

12:00 AM
3:00 AM
8:30 AM
11:50 AM
1:00 PM
5:05 PM
11:25 PM

24 Hour Clock


Military time


in HomeKit


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 DateSelect Date and you’ll be able to choose between Variables and Current Date. We want to choose Current Date.

Format Date to number

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
   Do something
End If
If TimeNow is between 0 and 300
   Do something
End If


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.

11 thoughts on “The power of the Format Date function and how to use it for shortcuts using time of day or sunrise/sunset (part 1)”

    1. The simplest solution for doing anything going from a time before midnight to a time past midnight, is to make it the default, meaning if none of the other if-statements are true, you do the last thing. In that case you either need to do a nested if-statements with otherwise, or put an Exit shortcut with Result as the last part in each if-statement.
      Otherwise, you have to create two if-statements for that part like this:

      If TimeNow is between SunsetTime and 2359
      Do action X
      End If
      If TimeNow is between 0 and 200
      Do action X
      End If

      And then add a third if-statement for the other condition:

      If TimeNow is between 300 and SunriseTime
      Do action Y
      End If

      I’ll go into more detail about sunset and sunrise in part two (but you also got that in the answer I posted to your original question 🙂).

      1. Thank you so much!! Yes, I’ve read your previous comment and, I’ve finally figured it out. Now I’m trying to follow your turn lights off when no motion is detected guide. Hope it’ll all work out fine.

  1. Actually, I’m moving from Android to IOS and I’m in loving with Home Automation possibilities in IOS systems.
    I’m using Homebridge to expand my possibilities and until now, I did’t know how to use timestamp.
    Thanks for share this knowledge with us
    Greetings from Brazil

  2. Wow I cannot believe I haven’t came across this site until now, a lot of YouTube videos but I prefer written content, So thank you for this. Will be bookmarking your site.
    I’ll try using this with an NFC tag and lights.

  3. Hi there, thanks soooo much for explaining this in details. I really appreciate your sharing of knowledge. I was wondering how do we put it in Automation? OR will ios constantly checking if the shortcut is within the time frame before it runs? Again, Thanks soo much!!!

    1. Hi Doris,
      And thank you for your comment and question.

      The shortcut must be triggered some other way, for instance:
      a motion or contact sensor,
      a accessory is controlled (a lamp is turned on or off)
      people arrive or leave (if you are using geofencing)

      If you are using Homebridge or Hoobs, you could also use a scheduled dummy switch that triggers the shortcut at an interval, for instance every hour or every five minutes.

      The automation and HomeKit shortcut itself will run on your HomeKit Hub, which would be an AppleTV, HomePod (Mini) or even an iPad (least stable).

  4. Hi Stefan,
    I am not a HomeKit user but creating many other type of Siri Shortcuts to use in my car, iPad or iPhone, you know. There’s a crazy variety for these 🙂
    I was always thinking (and I tried few times) to add some time limitation to these automations that would be amazing but I couldn’t manage it.
    This morning, I searched it online and came across your page which I really think so successful and need to be more improved and developed because no doubt that there’s a light for this.

    I really appreciate and thank you.
    I guess there are plenty of things to learn from you about it.

Leave a Comment

Your email address will not be published. Required fields are marked *