# Help with error in code

**URL:** https://discourse.nodered.org/t/help-with-error-in-code/91109
**Category:** General
**Created:** [19 September 2024 12:21 UTC](https://discourse.nodered.org/t/help-with-error-in-code/91109 "2024-09-19T12:21:58Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [19 September 2024 12:21 UTC](https://discourse.nodered.org/t/help-with-error-in-code/91109/1 "2024-09-19T12:21:58Z")

</div>

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

```auto
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:

 ![Screenshot from 2024-09-19 22-19-57](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/9/391fab2a42c477a2b1a5dc510c4d1767254bb348.png)

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.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [19 September 2024 12:40 UTC](https://discourse.nodered.org/t/help-with-error-in-code/91109/3 "2024-09-19T12:40:44Z")

</div>

> [@Trying\_to\_learn](#):
>
> `const diff = now - startDate;`

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

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

```

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [19 September 2024 12:42 UTC](https://discourse.nodered.org/t/help-with-error-in-code/91109/4 "2024-09-19T12:42:52Z")

</div>

> [@jbudd](#):
>
> `const diff = now.getTime() - startDate.getTime()`

Thanks.

(Gotta love chat-gpt.) 😉

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.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [19 September 2024 15:05 UTC](https://discourse.nodered.org/t/help-with-error-in-code/91109/5 "2024-09-19T15:05:36Z")

</div>

> [@Trying\_to\_learn](#):
>
> but I am getting an error.

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.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [3 October 2024 15:05 UTC](https://discourse.nodered.org/t/help-with-error-in-code/91109/6 "2024-10-03T15:05:54Z")

</div>

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