Working with Dates in Bubble
Working with dates in Bubble can be a little bit tricky so here's my Bubble tutorial video showing you how we can filter a list of dates to only return dates in the last 30 days and if you're building a no code SaaS app with Bubble then click the link in the description because we've got hundreds of videos just like this one to help accelerate your Bubble app development journey.
Creating an Expression for the Last 30 Days
Right let's look at building an expression that targets the last 30 days. So I'm going to do it in this text field initially because then you can see each step in action. So we can say current date time and we can say days and it says plus days but you can use a minus operator in here.
Understanding the Initial Expression
This isn't quite it but I'm going to show you what this looks like because it's currently May 8th 11:30. Notice it is relative to time zone and so now we go back to April 8th which would be 30 days ago but it's saying 11:30 which is going to miss any events that happened on April 8th 10:30 and when a user is thinking last 30 days they probably want all of April 8th.
Refining the Expression
So we go back in here and we can just say and add to the end of it round it down to day because now if we look in the expression we should have April 8th midnight right at the start of April 8th.
Applying the Expression to Filter Data
So if we wanted to filter our calendar by that or it could be a repeating group just anything with date data like events we can say the start date that we want to have the start of our date sandwich is going to be our expression of current date time days minus 30 rounded down to day.
Handling Future Events
Okay and then so that we don't accidentally get any events in the future for example we could have the next 30 days but we wouldn't want that included. Well you've got to make a decision whether the last 30 days includes the day or not but effectively you could do something like this where is date is less than current date time.
Refining the End Date Expression
Now that's not perfect because if we wanted our last 30 days expression to include events that are in the future but are on today's date we're going to miss them here. So instead we could say days plus one rounded down to day.
Explaining the Final Expression
So what that's going to do is take my current day which is May 8th. Oh no I've days plus one one rounded down there we go. So that's going to take my current date which is May 8th plus one which would make May 9th at 11:30 and then round it down so that's going to be the midnight of May 9th and because we're using a less than and not a less than or equal to operator here it's going to be the fraction before the start of May 9th.
Conclusion
So that's going to work and that's how you can filter by the last 30 days in your Bubble app.