# Why does "new Date()" show as an error?

**URL:** https://discourse.nodered.org/t/why-does-new-date-show-as-an-error/88776
**Category:** General
**Created:** [18 June 2024 12:23 UTC](https://discourse.nodered.org/t/why-does-new-date-show-as-an-error/88776 "2024-06-18T12:23:49Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![daxliniere](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daxliniere/32/42187_2.png) [@daxliniere](https://discourse.nodered.org/u/daxliniere)
#### Post date: [18 June 2024 12:23 UTC](https://discourse.nodered.org/t/why-does-new-date-show-as-an-error/88776/1 "2024-06-18T12:23:49Z")

</div>

For some reason **new Date()** is identified by NR as an error, though the (function) node compiles and runs correctly.

Any ideas why this would be? The specific error message is:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/a/7a8a10563c9256aa4691fc8865b4c9eb6dbe6f6d.png)

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [18 June 2024 12:38 UTC](https://discourse.nodered.org/t/why-does-new-date-show-as-an-error/88776/2 "2024-06-18T12:38:55Z")

</div>

Because of the Date object is not a number. You can't do math with the Date object.

Use the value of the Date object

```auto
let a = Date.now() - 100;
let b = new Date().getTime() - 100;

```

---

<div class="post-metadata">

### Author: ![daxliniere](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daxliniere/32/42187_2.png) [@daxliniere](https://discourse.nodered.org/u/daxliniere)
#### Post date: [18 June 2024 12:50 UTC](https://discourse.nodered.org/t/why-does-new-date-show-as-an-error/88776/3 "2024-06-18T12:50:54Z")

</div>

But it does work anyway..?

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [18 June 2024 12:58 UTC](https://discourse.nodered.org/t/why-does-new-date-show-as-an-error/88776/4 "2024-06-18T12:58:56Z")

</div>

It's a bug in TypeScript used to parse the code and try to identify issues. It does not know that `new Date()` can be treated as a number. So whilst the code will work, the editor will flag it as an error. Nothing we can do about it ourselves until the upstream issue is resolved - although its been open for over 9 years....

> <https://github.com/microsoft/TypeScript/issues/2361>
>
> \`\`\` ts
> class NumberWrapper {
> constructor(private value: number) {}
> value…Of(): number { return this.value; }
> }
> 
> var x = new NumberWrapper(1);
> 
> // The right-hand side of an arithmetic operation 
> // must be of type 'any', 'number' or an enum type.
> console.log(2 + x);
> \`\`\`
> 
> It would be nice if an arithmetic operation could allow using the wrapped number because it's \`valueOf()\` method returns the expected primitive type.
> 
> This can be generalized to: if type \`T\` is expected, then a value that implements the following interface can be used.
> 
> \`\`\` ts
> interface Wrapped\<T\> {
> valueOf(): T;
> }
> \`\`\`

---

<div class="post-metadata">

### Author: ![daxliniere](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daxliniere/32/42187_2.png) [@daxliniere](https://discourse.nodered.org/u/daxliniere)
#### Post date: [18 June 2024 13:48 UTC](https://discourse.nodered.org/t/why-does-new-date-show-as-an-error/88776/5 "2024-06-18T13:48:27Z")

</div>

Thanks for the info, Nick!

---

<div class="post-metadata">

### Author: ![daxliniere](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daxliniere/32/42187_2.png) [@daxliniere](https://discourse.nodered.org/u/daxliniere)
#### Post date: [22 June 2024 11:39 UTC](https://discourse.nodered.org/t/why-does-new-date-show-as-an-error/88776/6 "2024-06-22T11:39:50Z")

</div>

Hey Nick, is there any way that we could set a node to 'ignore errors' or at least not notify upon Deploy? Every time I deploy, I get this annoying error warning message which I know isn't actually a problem.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [22 June 2024 11:50 UTC](https://discourse.nodered.org/t/why-does-new-date-show-as-an-error/88776/7 "2024-06-22T11:50:07Z")

</div>

No, there's no way to ignore the errors. Id suggest you just swap over to using `Date.now()`

---

<div class="post-metadata">

### Author: ![daxliniere](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daxliniere/32/42187_2.png) [@daxliniere](https://discourse.nodered.org/u/daxliniere)
#### Post date: [22 June 2024 12:08 UTC](https://discourse.nodered.org/t/why-does-new-date-show-as-an-error/88776/8 "2024-06-22T12:08:05Z")

</div>

> [@knolleary](#):
>
> Date.now()

Ah! I didn't know there was an alternative. Thanks so much.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [22 June 2024 14:23 UTC](https://discourse.nodered.org/t/why-does-new-date-show-as-an-error/88776/9 "2024-06-22T14:23:37Z")

</div>

> [@knolleary](#):
>
> No, there's no way to ignore the errors. Id suggest you just swap over to using `Date.now()`

Just for info.  
// @ts-ignore will ignore the error.

```auto
// @ts-ignore
msg.payload = new Date() - 100;

```

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [22 June 2024 16:07 UTC](https://discourse.nodered.org/t/why-does-new-date-show-as-an-error/88776/10 "2024-06-22T16:07:12Z")

</div>

But don't. Undocumented behaviors should never took as granted.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [22 June 2024 17:39 UTC](https://discourse.nodered.org/t/why-does-new-date-show-as-an-error/88776/11 "2024-06-22T17:39:57Z")

</div>

It was not an endorsement to use it or not, I will leave that to the end users own opinion and decision, it was simple for informational purposes, as noted.

---

<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: [6 July 2024 17:40 UTC](https://discourse.nodered.org/t/why-does-new-date-show-as-an-error/88776/12 "2024-07-06T17:40:55Z")

</div>

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