There must be a nice way of doing this
How can I find the date of “next Monday”? The only way I can think of is to check if current date/time + 1 day is Monday, and if not, try current date/time + 2 days. It just seems awkward!
There must be a nice way of doing this
How can I find the date of “next Monday”? The only way I can think of is to check if current date/time + 1 day is Monday, and if not, try current date/time + 2 days. It just seems awkward!
It might be easier to use an API for this. I’d imagine there are services out there that can return this information, and it’d be cleaner than doing what you’re proposing. Does Bubble know that March 10, 2018 is a Saturday?
I just did this for two separate apps. It’s not the most convenient, but you can do it.
It’s all in extracting the day of the week. Bubble has Sunday as 0, Monday as 1, etc.
You take current date + 7 days (to get you to next week) and then subtract the difference between today’s day of the week and 1 (Monday).
So if today is Monday:
Current Date +(days) 7 +(days) 0 = next Monday’s date
If today is Tuesday:
Current Date +(days) 7 +(days) -1 = next Monday’s date
etc.
To calculate the difference you can do “Current Date/Time :extract day - 1”
Monday would be 1-1 = 0
Tuesday would be 2-1 = 1
Wed is 3-1 = 2
etc.
Yes, it’s annoying, but you can do it. I’d be into an API that does this for you too…
I don’t want too much network connectivity for this. It’s something to be used in live interaction with the user, so it would be better to run everything locally. And Bubble can give you the weekday.
Thanks, this is helpful, although still a bit tedious
If you don’t mind using a little Javascript and a JS library, you could do the following:
Add this to your page header -
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>
Use the JavascriptToBubble element (Toolbox Plugin) to publish the result of the following JS code (found on stackoverflow):
var nextMon = getNextMonday ();
function getNextMonday () {
const dayINeed = 1;
// if we haven’t yet passed the day of the week that I need:
if (moment().isoWeekday() <= dayINeed) {
// then just give me this week’s instance of that day
return moment().isoWeekday(dayINeed);
} else {
// otherwise, give me next week’s instance of that day
return moment().add(1, ‘weeks’).isoWeekday(dayINeed);
}
}
var nextMon = nextMon.format(“MM/DD/YYYY”)
bubble_fn_mon(nextMon);
Example here: https://bubble.io/page?type=page&name=getmonday&id=louisforum&tab=tabs-1
Thank you very much, I will have a look.
Just wanted to add something small here.
@soeren, when you say “next Monday” do you mean exactly that or you mean “next week’s Monday”.
They’re two different things.
For example, if today is Sunday and you want “next monday”, it should be tomorrow. While “next week’s monday” should be tomorrow’s week.
@romanmg solution will give you “next week’s monday” while @louisadekoya solution for “next monday”.
To see why i say @romanmg solution is for “next week’s monday”, lets assume today is a Sunday (which is 0).
To calculate the difference using the formula “Current Date/Time :extract day - 1” will give -1 (i.e. 0 -1 = -1).
So the main function will be:
Current Date + (days)7 + (days)1 = next week’s Monday’s date. (NB: - -1 = +1)
That is you’re adding 8 days from Sunday which should be next week Monday.
This is why @louisadekoya solution has that “if…else” condition to check if we haven’t yet passed the day (Monday) that we’re interest in (assuming we’re on a Sunday).
Another thing to note is that, with @louisadekoya solution, if today is a Monday you will get today’s date (see the “<=” in his if… conditional). Depending on your requirements this might be desirable or not.
With @romanmg solution if today is Monday, your result will be the coming Monday.
So both solutions are correct and which one to go with depends on whether you meant “next monday” or “next week’s monday”.
If you don’t want to go the javascript way and use @romanmg solution to get “next monday”, you can have a condition that checks if “Current Date/Time :extract day -1” < 0, and just add the difference to today’s date.
If it >= then you do the +7 days that @romanmg did. Also the comparison operator you use (>= vs >) will depend on whether if today is monday you want today’s date or next weeks monday.
Thanks for the response.
This topic was automatically closed after 70 days. New replies are no longer allowed.