Days Until Function

Is there a function to get the days between a current date and a future day? I know that days can be added, but can they be subtracted?

My use case is to have my dashboard countdown the number of days until MLB opening day. I have both dates, just looking to do the math on the days inbetween.

I tried using datediff, but that didn’t work:

date = $context.response.data.leagues[0].seasonDateInfo.regularSeasonStartDate
today = formatDate(now(), "YYYY-MM-DD ")
datediff(d,date,today)

Am guessing there is an api I could call, but wondering if there is a function to call.

You can perform math on dates directly and then format those durations. The Date Formatting section of the Date Functions in Rule Expressions help article describes some of the formatting functions.

Keep in mind that to perform math on dates or to format them as durations, they must be in date/millis format, not strings. So be sure to use the date() function to parse them if necessary.

For example, humanizeDuration() gives you a human friendly version of the duration. It automatically does some rounding and other things to make things more human friendly, so depending on the level of accuracy you’re looking for, you may prefer one of the other solutions where you have more control over the formatting.

And durationAs() lets you get the duration in specific units that you choose.

Keep in mind that with durationAs(), you’ll often want to wrap it in a round() function as it’s going to give you the raw result with full decimal precision. So 22 days duration as ‘weeks’, would be 3.1428... weeks whereas you might only care that it’s 3 weeks or 3.1 weeks or whatever level of precision you’re looking for.

round(durationAs(duration, 'weeks'), 1)

Perfect – thanks!

I totally missed both the duration and the humanizeDuration functions.

1 Like