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);
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);