Need Help with JavaScript to Bubble Element for Calculating Working Days per Month

Hi everyone,

I’m working on a backend workflow in Bubble to calculate the working days for each month by subtracting holiday days from the total days in that month. Here’s the setup:

Data Overview

  1. Holiday Data (paramlist1):
    Each holiday includes:
  • start_date (Date)
  • end_date (Date)
  • month_number (Numeric representation of the month)
  • date_count (Total number of holiday days in that month: end - start)

Here’s the JavaScript I wrote in Run JavaScript action. I’m using the bubble_fn_calculate function to return the results back to Bubble:

var months = [
    { month: "January", month_number: 1, total_days: 31 },
    { month: "February", month_number: 2, total_days: (new Date().getFullYear() % 4 === 0 && (new Date().getFullYear() % 100 !== 0 || new Date().getFullYear() % 400 === 0)) ? 29 : 28 },
    { month: "March", month_number: 3, total_days: 31 },
    { month: "April", month_number: 4, total_days: 30 },
    { month: "May", month_number: 5, total_days: 31 },
    { month: "June", month_number: 6, total_days: 30 },
    { month: "July", month_number: 7, total_days: 31 },
    { month: "August", month_number: 8, total_days: 31 },
    { month: "September", month_number: 9, total_days: 30 },
    { month: "October", month_number: 10, total_days: 31 },
    { month: "November", month_number: 11, total_days: 30 },
    { month: "December", month_number: 12, total_days: 31 }
];

// Retrieve the holidays list
var holidaysLength = properties.paramlist1.length();
var holidays = properties.paramlist1.get(0, holidaysLength); // List of holidays

var workingDaysList = [];

// Iterate through the list of months
months.forEach(month => {
    // Find the holiday for the current month
    var holiday = holidays.find(h => {
        var startDate = h.get("start_date");
        return startDate && startDate.getMonth() + 1 === month.month_number;
    });

    var holidayDays = holiday ? holiday.get("date_count") || 0 : 0;

    var workingDays = month.total_days - holidayDays;

    workingDaysList.push({
        month: month.month,
        working_days: workingDays
    });
});

// Return the working days list to the Bubble workflow
bubble_fn_calculate(workingDaysList);

Problem is I dont know how to exactly show the result in the repeating group. Also don’t know if the code works correctly or not. Any help?

Hi @mzffreyvazov2005, there is a plugin for you to evaluate, if it is what you are looking for or maybe it can help you, here is the link: [Calculate Business Days · BEP Plugin | Bubble] ( Calculate Business Days · BEP Plugin | Bubble )

1 Like

Deleted my post, are you using backend workflows (server script) ?

Thanks, man. But I want to calculate the businnes days from the start of the month to the end of the month (and also subtracting the holiday days). The problem is, how can i specify the start of the month and end of the month in that plugin?

I tried to add a field to Holidays datatype, month_start (and month_end) and couldn’t find a way to select the start date of the month from AirDate/TimePicker. Because if I can do this, I can calculate the totla businnes days like this: result of calculate_businnes_days(month_start, month_end) - result of calculate_businnes_days(start_date - end_date).

I have a button in the page and when it’s clicked it runs javascript, and should return a list containing the months and their working days with javascripttobubble element.



Hello, @mzffreyvazov2005, I did a quick test (two images below), to bring me the number of days within a start date and end date element, then it brings me the days, but I selected it to start on Monday excluding weekends. I don’t know if this is the line you want. No plugin.


I added two more images showing that I am using the beginning of the week without weekends, and I am using a sum at the end in the subtraction calculation, so the day count will work correctly.

I did it with the airdatetime element that you used, but there are some differences: see screen below.

Hello, @krenkel. Thanks for your effort and attention.

This task was given to me by teacher and wants me to add that functionality. Here is the corresponding page where it should be implemented:


Note: Original language is not english, so the texts are translated by google translate

As you can see, user can add holiday dates (or non-working days as shown in the image) for each month, and at the bottom repeating group, it should show the total working days for that month.
For example, I added “holiday1” which is Nov 1, 2024 - Nov 15, 2024 (Note: My teacher wanted it to be a date range, so I used AirDate / TimePicker plugin).
At the bottom group, the total working days should be (30 - total number of holiday days - non businnes days, like saturday and sunday)

Have you added the JavascriptToBubble element on your page? You’ll need the element to retrieve the results of your code.

I tried to use javascripttobubble element, but couldn’t use the resulting list from javascript in the repeating group

Sorry i reread your post. It’s a backend action?

Yeah, it is backend. After the user add holidays with their dates, the bottom repeating group should show the total working days for each months

I’m a little confused. Maybe there’s a little misunderstanding. When you say backend you mean in your app’s “Backend Workflow” or the Workflow in a page?

Since you mentioned that a Repeating Group is involved, I assume you are actually saying a page’s Workflow?

I am sorry, but didn’t understand what you are trying to know neither).

What I want to do is: given the holiday dates for each month, show the user total working days for each month. And I think there would be several ways to do this. with javascript, backend workflows, or using a plugin. I thought using javascript would be best, but couldn’t find a way to do it, as shown in the post. Also used backend workflows, also again couldn’t do it.

Check out this link, I believe it has something for you from 2018. Calculate Working Days (Business Days) between a Date Range - Need help - Bubble Forum

I can apply this to the date range of holidays, bu not the full date range of each month, for example, Nov 1 - Nov 30. It seems that I should create another date picker just to pick the start and end of the month but it would seem unnecessary for the user.

1 Like

Okay no worries. Your script looks good to go but you will need to pair that script with Toolbox’s JavascriptToBubble element. Suffix the JavascriptToBubble element accordingly and you’ll get your results. To display the data in an RG just point the data source to that JavaToBubble element.

Read more here: Javascript to Bubble - Toolbox docs

1 Like