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

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:

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

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

But it does work anyway..?

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....

1 Like

Thanks for the info, Nick!

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.

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

1 Like

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

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

// @ts-ignore
msg.payload = new Date() - 100;
2 Likes

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

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.

4 Likes

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