Shortcut to define part of day

I want to write a helper shortcut that decides what part of the day it is (morning, afternoon, night). I want to start off with a dictionary that holds the start and end hours of each part so it can easily be tweaked later on.
I already have a simple helper shortcut that gets the hour of the date I add as input and I know how to repeat over the dictionary keys. But I am running into trouble testing if the hour is between the start and end of the date.

This is what I have so far:

  • create dictionary —> comes down to:
    {
    “Morning”: { “start”: 6, “end”: 12 },
    “Afternoon”: { “start”:12, “end”: 18 },
    etc.
    }

  • run shortcut ‘get hour of date’ with current date as input

  • get all keys of dictionary

  • repeat with each key

  • get value of ‘start’ of repeat item

  • get value of ‘end’ of repeat item

  • if shortcut result …

Here I’m running into trouble: I cannot choose something like ‘between’ or ‘is greater than’, only text based comparisons.

Also, I wonder if this is the most efficient approach. I have looked for a similar shortcut in the forums but I cannot find one.

Tap on the first variable in the if statement, and then change it from As Text to Number, that should offer the right comparisons.

1 Like

Try this shortcut to meet your particular timings requirements. Uses a dictionary and some loops, but not the same structure as you describe above.

1 Like

Wouldn’t it be easier to turn the dictionary around, using hours as the keys and “Morning,” “Afternoon,” “Evening,” etc., as the values? Then you just pull the hour value out of the time and look up the part of day in the dictionary. I realize this makes for a bulkier dictionary, what with all the repetition of values, but the logic becomes much simpler, with no looping or nested conditionals.

And for those of us who use a 12-hour clock, the keys would have to include AM/PM.

This approach specifically caters for scenarios that are not based on hours but down to the minute. That’s why I specified the evening to night switch occurring at 21:30 as a way to illustrate this.

If, and only if, you only wanted to cater for hours, then I agree, a dictionary of 24 entries would be easier to specify and simpler to process. But I don’t think the Shortcut above is overly onerous to set for your own periods.

On the “can’t use comparisons” thing you could left pad with zero characters. And then string comparison becomes “clean”.

Awesome. This is exactly what I was looking for. I love to have the minutes too. Adding those would have been my next step.
Thanks so much.

Another approach would be to get the difference between the current time and 12:00 AM. This would give a numerical value in minutes. Then it is a simple comparison. A value less than 360 (6:00 AM) is night and so forth.

If you take a look at the details of the Shortcut, you’ll see that it is doing something very similar. It is removing the colon in the time to get a numeric value rather than calculating an offset in minutes against a time to get a value.

I had a quick play to see if this could be done by making greater use of Shortcuts’ built in date functionality and came up with the following…

https://www.icloud.com/shortcuts/15adb565242d4b0c88ebacf26daa4f61

Differences include:

  • Just uses a text action to specify the parts of day (appreciate the original post called for a dictionary, but I think this makes the shortcut simpler)
  • Only specifies the start of each part of day as the end is implicit
  • Uses the built in date comparison functionality

All the times can be in 12 or 24 hour format depending on your preference. :slightly_smiling_face:

Not suggesting this is better than the solution posted by @sylumer, just a slightly different take on the problem.

1 Like

New here but I was wondering why no one converted the date to 24 hour format and then just used hour markers in simple if statements to define the part of the day.

Example:
https://www.icloud.com/shortcuts/67db2416cf6846ac8d0777ff14371474

It really comes down to the use case.

An approach based on If statements and hours would be fine in many circumstances and as you’ve illustrated would be shorter and simpler. The downside is that is hard coding the definition of the parts of the day and relying on these falling on hour boundaries.

The original post here was looking to be able to flexibly define/change the number of parts of the day and their time boundaries in a dictionary without having to amend the rest of the shortcut.

Later in the thread the requirement was added for the parts of the day to be defined on minute boundaries, not just hours.

This drove the dictionary / loop based approaches suggested by @sylumer and myself.

Ah. Got it. I found the thread because I was searching for a way to do this myself. I understand the additional requirements now driving those solutions.