Calculate a date (skipping weekends)

Hey there guys,

Ok so, this is the situation. I’ve got an app where one of our sales reps can feed in a date, and depending on the type of product (different data type), a product delivery date is calculated and shown.
The type of product data type is configured with a “Type” text field and a “Date interval” field that has the time that it takes to source, build and deliver the specific product.

The Product delivery date was supposed to be calculated when the order is being placed, so “Current time” + ProductType’s “Date interval” = Product delivery date. The problem is that the date interval does not take into account that in some cases, there will be weekends in middle of the interval, which should not be counted towards the calculation.

So, if a product type has a 3 day interval, and the order is placed on friday at noon, it should calculate the delivery date as wednesday at noon, not monday.

I have no idea how to get this done, any ideas?

Cheers and thanks,
Arcus.

Hi Arcus,

Here’s some javascript I found that does the trick:

function addBusinessDays(d,n) {
    d = new Date(d.getTime());
    var day = d.getDay();
    d.setDate(d.getDate() + n + (day === 6 ? 2 : +!day) + (Math.floor((n - 1 + (day % 6 || 1)) / 5) * 2));
    return d;
}

Get the free Toolbox plugin added to your app, then add an expression element to your page:

Along with the function from above, we’ll set some dates, etc. and the final thing looks like this:

You are just setting an startDate from a date picker (or some other date in Bubble) and also setting the desired interval. The expression element will return a date which you can use just like any other Bubble date.

Good luck!

–Ken



Looking to accelerate your app development?

Development of Advanced Apps at https://uniqueideas.com or schedule a free intro session :gift:

Ken Truesdale
LinkedIn

1 Like

Awesome, thanks a lot @mebeingken that’s exactly what I was looking for!

Cheers,
Arcus.

1 Like

This topic was automatically closed after 70 days. New replies are no longer allowed.