Dates in bubble.io and JS

Hey everyone,

I’m having an issue with handling dates in Bubble.io and JavaScript.

I have a JavaScript function that returns all the dates within a one-month period starting from a given date. For example, I have a date October 23, 2024, 11:00 AM – Wednesday, and I need to find all the Wednesdays within month.

However, the function returns the dates, but the time is one hour earlier (e.g., October 30, 2024, 10:00 AM, November 6, 2024, 10:00 AM etc.). I need time to be exactly the same as it was (11:00 AM).

Has anyone encountered a similar issue or knows how to fix it? Any help would be greatly appreciated! Thanks in advance.

Here is a JS code:

function getAllSameWeekdaysForPeriod(startDate, periodInMonths) {
    const resultDates = [];
    const endDate = new Date(startDate);
    endDate.setMonth(startDate.getMonth() + periodInMonths); // Set the end date to the specified period after the start date

    let currentDate = new Date(startDate);

    // Collect all dates with the same weekday within the period
    while (currentDate < endDate) {
        resultDates.push(new Date(currentDate));
        currentDate.setDate(currentDate.getDate() + 7); // Move to the next week
    }

    return resultDates;
}

// Example usage:
const date = new Date('start-date:formatted as 2024-10-23T20:45:52.317Z');

return getAllSameWeekdaysForPeriod(date, 1);

Your input time is in UTC.

'2024-10-23T20:45:52.317Z'

But the rest of your code is using local system time.

e.g.

endDate.setMonth(startDate.getMonth() + periodInMonths);

So if you input a time of 9:00pm (in UTC) in your timezone (which is presumably 1 behind UTC?), that will be 8:00pm in the system’t local time.

So, either set the initial start date in the local system time zone, or keep it as UTC, but set all the dates in the code in UTC as well.

Thank you for your response. I still get the same issue, event though I corrected the code

function getAllSameWeekdaysForPeriod(startDateInput, periodInMonths) {
    const resultDates = [];

    const startDate = new Date(startDateInput);
   
    const endDate = new Date(startDate);
    endDate.setUTCMonth(startDate.getUTCMonth() + periodInMonths); // Set the end date

    // Collect all dates with the same weekday within the period
    let currentDate = new Date(startDate);
   
    while (currentDate < endDate) {
        resultDates.push(new Date(currentDate));
        currentDate.setUTCDate(currentDate.getUTCDate() + 7); // Move to the next week
    }

    return resultDates;
}

// Example usage:
const date = new Date('start-date:formatted as 2024-10-23T22:13:50.865Z');

return getAllSameWeekdaysForPeriod(date, 1);

The Dates in your code are still being set in local time:

e.g.

const startDate = new Date(startDateInput);

Here you’re taking the startDate argument (which is a date defined in UTC), and creating a new Date object in the local system time.

e.g. if the Start Date is 8:00pm in UTC, that’s 7:00pm in the local system time… so that’s the start date being used.

If the input date is in UTC, and you want to keep all Dates in UTC you need to set all dates as UTC Dates…

note: even if you output the list of Dates in UTC, they will still appear different to you when viewing them in your local timezone.

What exactly is it you need as the output here?

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