Rules / Variables based on Date or Calendar

I wanted to a share a workaround that might be sufficient until we get a chance to build this as a first-class feature in the Rule Engine.

:stop_sign: Update: Math / Date Expressions are now available as a native feature. You can use the syntax noted below directly in an expression without having to use the Lab environment.

:link: Feature Announcement: Math / Date Expressions

We have a Lab environment which has some endpoints that can be called using the HTTP Action in the Rule Engine and you can get the results using context variables. The /date endpoint can be used with an optional timezone (IANA format) and optional locale parameter to customize the output.

https://lab.sharptools.dev/date?timezone=Poland&locale=pl

As youโ€™ll see from the link above, this provides a wide variety of date related details available as properties that you can then use in your rules.

Formatted Output

Furthermore, you can pass a format property using any combination of those values to receive a fully formatted string in return. Examples:

Note that if you are using more complex formats with spaces and such, you will either want to URL encode them or you can even use a POST request to send the parameters across.

Localized Formats

Also supported for the format field are some default localized formats:

  • L - localized date (short)
  • LL - localized date (long)
  • LT - localized time
  • LTS - localized time with seconds

Formatted Input

You can also provide an input to the /date endpoint as well.

  • input - expects an ISO 8601 timestamp by default, but can be modified with inputFormat
  • inputFormat - the format of the input value (eg. a format string like YYYY-MM-DD or MMMM DD, YYYY)
  • inputTimezone - the IANA timezone (if your input value is not UTC)
  • inputLocale - can only be used if youโ€™re using the default UTC timezone (eg. no inputTimezone)

Date Between

The /date/between endpoint accepts a start, end, and timezone (IANA) property for checking if the current date is between two dates.

For example, you could check if the date is between August 1st and September 1st
https://lab.sharptools.dev/date/between?start=2022-08-01&end=2022-09-01&timezone=America/Chicago

The formats are YYYY-MM-DD or MM-DD. By default, the endpoint will ignore the year โ€“ this enables the same condition to continue to work year after year without needing to update the rule. You can include the flag &includeYear=true if you want to restrict to a specific year.

The main property you would look for in the response is result which is a true/false indicating if itโ€™s currently between the specified date range.

You could use this with a rule that has a Time trigger to run every day and checks this endpoint to determine if it matches your range to decide if you want to run the rest of the rule or not.

1 Like