Forum Academy Marketplace Showcase Pricing Features

Condition for todays Date (but not time)

My users can create events. I want to add a condition to display an user “event” based on today’s Date like this:

Date = Current date/time

But, this is too specific, I want to show events happening ‘today’ and ignore the time. Any way I can do this?

You will need to extract date. See Reference | Bubble

Remember that a ‘date’ in Bubble is a time-stamp of a specific moment, to the nearest millisecond.

So if you search for events who’s date matches the current date time, ONLY events who’s date is exact same millisecond as when the search was done will be returned (which will almost certainly be none).

So if you want to show events that are running Today, you need to use 2 constraints on the search:
Date ≥ Current Date/Time: rounded down to day
Date < Current Date/Time: rounded down to day: +(days) 1

1 Like

Thank you Adam :grinning:

1 Like

This works when the button “show events today” is pressed, but then when you release the button it reverts to showing all events. I simply put the condition on the RG so maybe I need to put the RG in a new group and treat it liked tabbed content so it remains viewable after clicking?

Also, I can see why I need this part;
Date ≥ Current Date/Time: rounded down to day

but I don’t see why I need this part;
Date < Current Date/Time: rounded down to day: +(days) 1

Surely the second part is referring to expired events which should not be shown anyway. It is strange because the first part won’t function properly without the second so I admit it is definitely required, but why?

What condition did you use on the RG?

When: Button Events today is pressed
Data source: Search for Groups

Date ≥ Current Date/Time: rounded down to day
Date < Current Date/Time: rounded down to day: +(days) 1

This is the page (see below) with the button which needs to show events ‘today’. As stated, it works when holding the button down, but as soon as it’s released we see ALL events again. Perhaps the issue is because I also have a dropdown filter which may be conflicting?

As I suspected :wink:, so there’s the issue…

The condition ‘When button is pressed’ is only true as long as the mouse button is held down, and no longer true when it’s released, so that won’t work for your case.

You’ll need to use custom states instead.

Personally, I wouldn’t use conditional data sources for this (although you can do, but it’s more complicated than it needs to be).

A simpler way would be to set custom states containing the start and end dates, then apply them as the constraints of the main data source search.

Then just set the state values when the buttons are clicked (i.e. when the Today button is clicked, set the start date to current date/time: rounded down to day and the end date to current date/time: rounded down to day: +Days 1

For the ‘Tomorrow’ button do the same thing, but set the start and end dates to tomorrow.

When the ‘Any Date’ button is clicked set the state values to empty.

Make sure to check the ‘Ignore empty constraints’ box on your data source search so that ALL events will be shown when no start or end dates are specified.

Also, I can see why I need this part;
Date ≥ Current Date/Time: rounded down to day

but I don’t see why I need this part;
Date < Current Date/Time: rounded down to day: +(days) 1

Surely the second part is referring to expired events which should not be shown anyway. It is strange because the first part won’t function properly without the second so I admit it is definitely required, but why?

The second part makes sure that only events for today are shown. Otherwise you’re just searching for all events that are on, or after, today (i.e. all future events).

I created a new custom state called “show_today”. But, I already have a custom state so when I try and refer to a new one in the data source, it just asks me which custom state I want (out of the two).

435

No, you’ve misunderstood…

You need to set the dates as constraints on the search (in the data source)

A start date and an end date.

Use custom states to set and hold those dates depending on what your user selects.

So have 2 custom states (date type) somewhere (for example on the page): 1 called ‘Start Date’ 1 called ‘End Date’

On your search, have 2 constraints for date:

Date ≥ Index's Start Date
Date < Index's End Date

Make sure ‘Ignore Empty Constrains’ is checked.

Then just set your custom state values accordingly when your ‘Any Date’, ‘Today’ or ‘Tomorrow’ buttons are clicked.

I have now done this:

I don’t know where to do this. Sorry, I seem to lack basic knowledge. I’ve tried to learn about custom states but something is really not clicking for me! See below, I am unable to refer to the custom states. Apologies for my misunderstandings.

Just click the ‘click’ button in the constraint window (in red), and select ‘Date’ (assuming that’s a field on your Group datatype).

Ah, I see. Ok that is now done:

Two issues though,;

  1. ‘When button is pressed’ is only true as long as the mouse button is held down, so still not working.

  2. I can’t see how to add the custom dates in the default value (below) because it won’t allow anything other than an exact date like 01/01/2021, I obviously need to be referring to group dates, rather than one fixed date.

Exactly, so as I said before, you can’t use that condition here, so get rid of it (it’s not going to work for what you’re trying to do - you need to use custom states instead).

Using multiple conditional data sources can get complicated and, although it’s sometime necessary, I don’t think it is here - you should be able to make this much simpler and just use a single data source, and a single search, constrained with some custom state values.

  1. I can’t see how to add the custom dates in the default value (below) because it won’t allow anything other than an exact date like 01/01/2021, I obviously need to be referring to group dates, rather than one fixed date.

Annoyingly, Bubble doesn’t let you set dynamic default values for custom states.

But that’s irrelevant here - you don’t need a default value as you’ll be setting the state values in your workflows and, presumably, you want the values to be empty on page load so that all events will be shown until the user selects otherwise (if not then just run a page load workflow to set the values to what you’d prefer them to be).

OK, I’ve removed those.

Understood. Thanks.

I have found a YouTube video which walks through various custom state examples, so I will spend this evening trying to up my knowledge! I understand the basics, create custom state, make RG refer to that custom state, then add workflow to the button with rules for the custom state.

1 Like

How can I use a custom state when I already have a custom state active in the RG Data source? Basically, I am unable to refer to a second state and there is no and function to help me add a second custom state.

cs

You seem to be rather confused about what you’re doing here… so I’ll try to clarify some basic concepts…

A repeating group is used for displaying data about a list of things.

Therefore the data source for a repeating group must be a list.

In your case, that list is the result of a database search of a particular data type (that you specify in your search, and must match the cotent type of the RG), constrained by whatever search constraints (if any) you apply on the search.

Then, in your data source you’re limiting the returned list of database items to a defined number of items, by using the ‘items until#’ expression, which must be a number (in your case a custom state value).

That’s your data source (I.e. the list of things you want the RG to display)…

So trying to add a ‘date’ somewhere in the data source doesn’t make any sense at all, and won’t work.

The start and end dates need to be applied as constraints on your search (whether those values are static, or dynamic).

As I suggested, the simplest way (in my opinion) to set those dates is by using custom states, set when one of your buttons is clicked.

I would also do the same for the dropdown menu for locations.

Have a custom state for location, and set it to the dropdown’s value whenever the dropdowns’s value changes.

But, if the dropdown’s value changes to ‘All Locations’ then set the value to empty.

That way you only need to have a single data source on the RG (rather than multiple conditional data sources) and you only need to do a single search, which is much simpler and cleaner than using multiple conditional data sources, and doing multiple searches.

So leave your current data source as it is, and apply the constraints for location, start date, and end date on the search itself (using the custom state values as those search constraint values).

Then use workflows to set those state values when location and dates are selected by your user.

I do appreciate your clear explanation, but this doesn’t make sense to me. I can’t see any examples in the documentation of how to assign more than one custom state to a single RG. Effectively, when you click on a RG, you can click on it’s data source and refer to one (and only one) custom state?

I have created start_date and end_date custom states on the main home page but how can I make the RG refer to them if I can’t add any more info to the RG data source?

Example, I now go to the ‘Events today’ button and start a work flow, asign the custom state but that custom state is not ‘attached’ to the RG?

Screenshot 2021-07-19 102910

Then, when I click ‘value’ I can’t see how to add the start_date info, see below;

I can’t see any examples in the documentation of how to assign more than one custom state to a single RG.

You can assign as many custom states to an element as you like (as far as I know there is no limit):

It’s worth pointing out again that it really makes no difference where you assign the custom states - it can be on the page, the RG, any other element - it really doesn’t matter as you can access them from anywhere (the only exception to that is when setting states on elements inside RG cells).

Effectively, when you click on a RG, you can click on it’s data source and refer to one (and only one) custom state?

I don’t really understand what you’re asking here… in the RG Data Source you need to define a list. If that list is defined in a custom state then you can refer to that custom state value.

Or, if you have a custom state of ‘Number’ type you can refer to that in things like item#, items until# etc.

But it doesn’t make any sense to apply a ‘date’ onto the RG’s list (its data source) so I’m not entirely sure what you’re trying to do there.

You need to apply the dates, and location as constraints on your database search.

I have created start_date and end_date custom states on the main home page but how can I make the RG refer to them if I can’t add any more info to the RG data source?

Refer to them in the search.

Example, I now go to the ‘Events today’ button and start a work flow, asign the custom state but that custom state is not ‘attached’ to the RG?

Again, it makes no difference where the custom state is attached to. But if you’d prefer then attach it to the RG instead of the page (it has no consequence at all).

Then, when I click ‘value’ I can’t see how to add the start_date info, see below;

You need to set the custom state value to whatever ‘Start Date’ you want it to be.

I.e. for ‘Today’ it should be Current date/time: rounded down to Day

I’ve created a quick example app that I’ve set up to achieve what you’re trying to do, so you can see how to do it for yourself (and how simple it is).

I’ve attached the custom states for ‘location’, ‘start date’, and ‘end date’, to the RG:

image

Here’s the link to preview so you can see how it works:

Event Test App (bubbleapps.io)

And here’s the editor link so you can see how it’s set up:

Event-test-2021 | Bubble Editor

Take a look and see how I’ve set everything up so you can get a better understanding of what you need to do and how.

You are awesome mate for going to the trouble of doing that, thank you!

I see how you’ve approached it and I’ve updated my app using the same custom states. The only difference is that where your custom state ‘location’ refers to location, I use an Option Set called “UK Towns and cities” so I refer to that.

The result my end is that nothing works or displays at all. I suspect this is because I am referring to my “UK Towns and cities” incorrectly somehow?

I’ve made my app viewable and wondered if you can see what I’ve done wrong? I don’t want to take the piss and get you to troubleshoot too much! I have gone through everything I can think of!