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).
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).
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.
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
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.
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 You can convert this to hours with simple multiplication
Thanks a lot @mitchbaylis …the idea is good BUT 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 .
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 You can convert this to hours with simple multiplication
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
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.
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.
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