Best way to calculate dates in conditionals

Hey everyone

So currently I have a repeating group that shows a list of tasks with due dates and I’m trying to customise how dates are viewed. Yesterday, Today, Tomorrow, In 6 days

Now currently I have conditionals that calculate when due date < currrent date, when due date is current date, when due date > current date and due date < current date: days +1 etc

Obviously this in a repeating group causes a lot of calculations and could slow down a large repeating group.

I’m thinking the best way to go about this is to store 2 dates called yesterday and tomorrow.
Then I can just check if due date is yesterday, current date, tomorrow or > tomorrow. These have to be formatted every time though to check if it is this date.

Does this seem like the most optimal way to do this or does someone have something that would be even quicker?

I think there are some relative time plugins that do this for you :slight_smile:

Don’t know why I didn’t think of that.

I’ll have a look in to it and try see if that’s quicker than what I already have.

Thank you

1 Like

Just for anyone looking for this relative time moment is the plugin in want.

Not going to work for my specific case. It also has something funny cause of how Bubble stores default times at midnight. Something that should say Yesterday in my case will say in 2 days. But it is a great plugin all round.

Hi @jay11,

I don’t quite understand what you mean by “have to be formatted”. Can you elaborate?

If I understand correctly, it sounds like you’d just need to store three reference dates against which to compare the current date. The reference dates can be stored in custom states.

When I think “formatted”, I think of converting to text, and dates certainly don’t have to be converted to text in order to compare them. I might be misunderstanding what you meant, but it sounds like it should be easily doable in pure Bubble.

Yes you’re correct in your understanding that I’m turning this into text. So the problem is that you can only go is it > or < than a certain date. You can’t do due date is tomorrow (stored state: current date:+days 1). You have to do due date:formatted as 2/23/22 is tomorrow( stored state: current date:+days 1:formatted as text)

Currently I have done what you’re entailing though is create states of yesterday and tomorrow . But I have to store 2 states for each one as text and one as date. Cause the options I want are before yesterday, yesterday, tomorrow and after tomorrow.

My goal is the lightest workload on the repeating group I can, so if I go and have a bunch of states stored with a date. I’ll need 2 calculations in a conditional to check for tomorrow and yesterday. Please let me know if any of this is wrong but that’s how I’ve figured it out.

No, you most definitely do not. That’s very inefficient.

Bubble date data types (which actually contain both the date and time) are really just integers under the hood - really big integers, but integers nonetheless.

A date simply represents the number of milliseconds that have elapsed since a particular reference time in the past (which is referred to as the Unix epoch and just happens to be 12am on Jan 1, 1970 UTC).

When you do +(days): 1, you’re really just adding 86400000 (which is the number of milliseconds in a day) to that big integer. What this means is that when comparing dates, you’re just comparing two integers, which is very fast.

In your case, you’d just store Bubble dates in the custom states and compare them to the Current date/time. The key is to set the reference dates to the specific moment in time that you need.

For example, if one wanted to see which books are due to the library on or before 5pm tomorrow, the reference date would be…

tomorrow-at-5p

That’s what would be stored in the custom state and equates to exactly 5pm the next day. Then, a conditional could be created for an RG list of books which would compare the due date to that value. If the due date is less than or equal to reference time, it’s due on or before that date/time. If it’s greater than the reference date (tomorrow at 5p), then it’s not due by then.

This little demo shows what can be done with relative time in pure Bubble.

-Steve

Thanks for the reply

I get what you’re saying and it’s good to know that formatting is an inefficient method. But that doesn’t solve the reason why I’m formatting. I want to check that two dates are the same day. Bubble doesn’t allow a simple default option after a date type to check if 2 dates are the same. So would the fastest way to do it since bubble stores UNIX as default be due date:extract as UNIX is tomorrow( state: current date:extract as UNIX) be a more efficient option?

I’d imagine this could be faster as it shouldn’t actually need to extract anything as this is the actual value stored. Luckily all my dates are stored as a bubble default so its midnight of that day, so this is a viable option for me.

Perhaps it’s helpful to think of a day as a span of time bounded by 2 dates. A day starts at 12:00:00am and ends at 11:59:59pm (two separate Bubble dates), so the sure-fire way to determine if some arbitrary date is “tomorrow” is to check if it falls within that range for the following day.

One way to do this is using the date range data type…

  1. First construct the date range

  2. Then create a conditional to check if a date is within that range…