Get date interval value in expression (without db storage)

Searched through the forum and couldn’t find the solution.

I have two “Date/Time” inputs and I want to get the value of the interval between the two without having to write the result into the database; In expression only. How can I achieve that?

Tried to play with “<-range->” and going with a simple “DateA - DateB” but couldn’t get to a working result…

Thanks in advance for any help.

1 Like

Another way of measuring the distance between two time values is to have date 2 (ie. end time) - date 1 (ie. start time): formatted as seconds//hours/days. You can apply some other functions (ie. divide by) to smooth it out.


Dan (creator of LearnTo - 15+ hours of Bubble tutorials and live coaching)

Hi Dan, thanks for anwsering quickly.
What you suggested was my first approach but I got stuck because Bubble somehow rounds the results even though I don’t ask for it.

Say my two dates inputs are the same date and the time only have 15 minutes of difference. The result “format as hours” should be 0.25, but instead I get 0.3 (18min instead of 15).

I couldn’t figure how to get the real value so I thought that there should be a better approach in the first place.

Please share a few screenshots of your expression setup. My recommendation is formatting as seconds, then doing the backwards math division and rounding to get the price value returned that you’re looking for.

I made a little app to try out your solution. I got it working but there’s a difference in how Bubble compute results:

12h15 - 12h00 in hours = 0.3h (wrong)
12h15 - 12h00 / 60 in minutes = 0.25h (right)

Here’s a link to the app:
https://tempjulien.bubbleapps.io/version-test?debug_mode=true

Thanks for building the example app. I see what you’re saying. Try exploring a bit with the inspect tool to see if it’s giving any hidden clues…

Mind changing the increments in your demo there to 5 minute (or 10 minute) intervals instead of 15? I’d like to see how it reacts to smaller increments.

Now it’s set to 5 min interval. The problem is that the hour decimal is rounded to the 10th.

5 min = 0.08h, rounded = 0.10h
10 min = 0.17h, rounded = 0.20h

In the app I’m developing, now the durations are expressed in seconds and convert to hours/minutes at the end. Works perfectly.

Paging @keith - what am I missing here?

Note: i just noticed something odd. 15 and 20 minute markers both show .3 hours; 45 and 50 minute markers both show .8 hours.

Hi @dan1… speak of the devil and in he walk.

So, what you and @julienallard1 observed is documented behavior. Please see:

(:to hours for date intervals is described right below there)

It might be helpful to scroll up from there, actually and read about the “date interval” type. When you subtract one date from another, this creates an object of “date interval” type (yes, it seems to be its own data type in Bubble)… as you can see here:

A date interval is a unit of time and, thus, is a rather handy thing to have in Bubble! It is described as follows in the docs:

Date interval type

This type represents a difference between two dates. It represents a precise number of milliseconds, rather than a calendar-friendly quantity such as “one day”. Adding and subtracting Date intervals always performs absolute math, not calendar-aware math. See the Date type above for more information on the difference between calendar-aware math and absolute math.

There is only one way to create a date interval: By subtracting one date from another. (Sort of like the ← range → constructor.)

Anyway, the descriptions of the date interval operators, point out the precision levels of each:

:format as days

Formats the difference as a number of days. We calculate this by dividing by 24 hours, then rounding to the nearest tenth (so 298944000 milliseconds becomes 3.5 days)

:format as hours

Formats the difference as a number of hours. We calculate this by dividing by one hour (i.e. 3,600,000 milliseconds) and rounding to the nearest tenth

:format as minutes

Formats the difference as a number of minutes. We divide through by 60,000 milliseconds and round to the nearest whole number

:format as seconds

Formats the difference as a number of seconds. We divide through by 1,000 milliseconds and round to the nearest whole number

So, that’s where all of that was coming from.

The precision of a date interval itself is milliseconds, as explained in the reference. So, yes, if you don’t like the :format rules for date intervals, you can just do the math yourself for complete control.

Don’t forget that operations like :format as return a string (text). Using such things is not the same as taking a date interval, dividing it by some number, and shoving that in custom state.

^^^ It would be worth testing: What data type is a date interval divided by a number? Does that yield a date interval? Or does it yield a number? NOTE: These things are different! A date interval is not a number representing milliseconds, it is in fact some amount of milliseconds. We could describe it as “time”. It is not, itself, a number.

(I haven’t tested this, but I’m pretty sure that date_interval / number yields a date_interval.)

3 Likes

And, BTW, my screenshot in the reply above answers the original question: you can of course create a custom state anywhere on the page, set its type to Date Interval and then set its value with:

DateX - DateY

Note again, date intervals are units of time. As units of time (at least in our universe) are only positive values (there is no such thing as negative time), the result of a date subtraction always has a positive sign.

The date interval represents the how much time is contained within the gap between DateX and DateY.

EDIT 2: Apparently you can do math on date intervals, but you can’t do it right in the value field of “Element > Set State”… Hmm, I’m not sure where you’re supposed to do that then.

2 Likes

Note to self: Seems there’s an opportunity for a lil’ plugin here, I suppose.

1 Like

I ended up using the “Expression” element from Toolbox plugin to extract/convert/adjust the time duration between my two date inputs. Then I show them up on screen using dynamic “Text” element.

It was very a very complicated process for a simple thing but it ended up working.

It would be so much simpler to have a customized “:format as…” instead of just “days/hours/minutes…”
Like: “Date Input A” - “Date Input B” :format as custom(HH:MM)

Yeah, I don’t quite get why there are no addition/subtraction operators after some_date_interval:format as...

It sort of looks like the implementation of the date interval type is incomplete, maybe? I note that “date interval” is not a selectable data type in the plugin builder, as another strange example.

1 Like

This topic was automatically closed after 70 days. New replies are no longer allowed.