Help with error in code

Sorry, I tried Chat-GPT and the code it gives me looks good - in theory - but I am getting an error.

const startDate = new Date('2024-9-17'); // Replace with your start date
const now = new Date();
const msPerDay = 1000 * 60 * 60 * 24;
const msPerWeek = msPerDay * 7;
const interval = 4 * msPerWeek; // 4 weeks in milliseconds

// Calculate the difference in milliseconds
const diff = now - startDate;

// Check if the difference is a multiple of 4 weeks
if (diff >= 0 && diff % interval < msPerDay) { // Check if within the day
    //msg.payload = `Message generated for the event on: ${now.toISOString()}`;
    msg.payload = "Recharge phone tomorrow";
    return msg; // Allow the message to pass
}

return null; // Do not send a message

What I see:

I understand what it is saying, but can't see the problem.

const now = new_date() sets now.
And startDate....
It is declared/defined.

So how do I get rid of the error?

Thanks.

Because both now and startDate are date objects not numbers, I think you need

const diff = now.getTime() - startDate.getTime()

Thanks.

(Gotta love chat-gpt.) :wink:

Yeah, I guess it is still early days.

(Saw/found another problem, but that isn't such a big deal.)
I'll work on it.

Note that you can tell chatgpt that you get an error and (hopefully) it will fix it.
Often, it is easier to write the expected output (with an example) and what you want to provide as input and it will reverse engineer what you are looking for.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.