Number of hours from time difference between two dates

Hi guys,

I am trying to get the number of hours between date 2 (later in the week let’s say) and day 1 (earlier in the week) and save them in the booking table as “booking_hours_used”. I will have this for every booking of course.

I have saved states which contain “current date” for each action. One for when people check out (date 2) and the other one for when people check in (date 1). I tested each date, one at a time (date 1 and then date 2) in that hours field which is of type “date” and they do get saved.

However, when I put the formula below, the formula doesn’t work.

THe ultimate goal is to get hours and minutes in that database field called “booking_hours_used” so I can call them in other elements (text, cards and so on).

Please help!!

1 Like

You are getting this error because the expression is apparently wrong due to the order of the formatting operator.

The expression you are using is Data2 - Data1 : formatted as hours, but here you are formatting Data1 in hours which prevents the correct calculation.

The operator must be used after the calculation, so ( Data2 - Data1 ) : formatted as hours. You may need to enable the use of parentheses if you have not already done so (Go to Settings> Version > Experimental Features > Expression parentheses).

1 Like

HI. @Newed ,

Thanks a lot for the help, but it didn’t work.
By the way, the booking_hours_used field…does it have to be other type than date? It’s currently date type.

any other ideas?

Perhaps the last is formatted as hours meaning it is text? Simply subtract second date which is utc time which is milliseconds. So you should be able to get the difference in Milliseconds and convert it to minutes or hours

Sorry, I m not sure I understand. They’re both dates format for sure.

What is the data type for booking_hours_used field?

If it’s text or number, then the result of your calculation needs to match that. If I remember correctly, using :formatted as returns a text. Therefore, first things first is that you need to make sure your field type and the type of your calculation is the same.

Next up, you’re going to want to use the extract from date operator: Operators and comparisons | Bubble Docs

Date2:extract from date (choose UNIX) - Date1:extract from date (choose UNIX). The result of this calculation will give you a number. This number represents the length of time in milliseconds :slight_smile: You can convert this to hours with simple multiplication

Have a good day :blush:

1 Like

format as hours is a number output so you need the field to be a number

as long as checkout and checkin are dates and the booking hours field is a number it should be correct

1 Like

Yeah, you need this to be a number-type field since you need a number of hours.

1 Like

Thanks a lot @mitchbaylis …the idea is good BUT :slight_smile: I will need to get that number and put it into a countdown. So I need to save hours and seconds. I think if I store into a number field it will just give me an integer and not hh:ss .

1 Like

Let us know what works for you!

1 Like

Just saw this, will try it.
Date2:extract from date (choose UNIX) - Date1:extract from date (choose UNIX). The result of this calculation will give you a number. This number represents the length of time in milliseconds :slight_smile: You can convert this to hours with simple multiplication

1 Like

Thanks a LOT @jonah.deleseleuc ,

It worked BUT…the number saved in miliseconds doesn’t look right. IT’s way to high.
so for aproximately two hours (difference), it saves 1740991723041 miliseconds which is 483K hours :slight_smile:

Something is fishy. This is the formula I Use to saved the UNIX ms time difference. The field in the DB is number.

and this is the formula i use to decode the milliseconds to hours and minutes

could just use a countdown plugin and save yourself the hassles

1 Like

I have it, but do you know how to make that time remaining persistent?! I tried that and once I leave the page where the counter is displayed, the info from the counter is lost. That’s why I’m saving the time remaining in the DB.

Or do you have a way for that counter to remember the time left? for me it didn’t work.

P.S. The user can check in again and contineu the time if it has time left in the booking so I still think that booking_hours_used is needed.

@adig I think you over complicate it. Bubble stores date (which is a moment in time), in Unix which is a number. Keep it that way so make sure both are date type fields. Do your thing with it and after translate the difference between the two to hours.

1 Like

Are you talking about the countdown or hours left?

If it’s the hours data,that’s what I’ve done, the only difference is I m saving the hours left in a field in the DB. I need that for the user to resume the time. I still need help for the above.

Forge about the hours left, all you need for timer is the end date.

So just store that in the DB.

2 Likes

Thanks @adamhholmes , great point BUT…in my case, after the booking is confirmed, the counter starts…
The counter is calculated based on hours booked initially and a state after the confirm button is pushed which contains the current date and time. That’s all good and simple and works fine.

However, when I leave the booking detail page where the counter is shown, it disappears, and i don’t think it runs in the background so that time exhausted is lost.
Am I missing something? Cheers

I’m not sure I fully understand the question/problem.

A timer is just the real-time calculated difference between 2 dates (one static and one changing)

Time doesn’t stop running.

You just need to store whichever of the 2 dates is the static one.

2 Likes

Exactly. Current date time - static time. Extract hours. Bobs your uncle

3 Likes