Arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type

Hi Forum,

I have a function node with the following code which runs fine and does what I want. msg.date contains a timestamp.

let age = (new Date() - new Date(msg.date)) / (1000 * 60);

but since the new Node-RED update I receive the following error-messages

"new Date()" is marked with this error:
The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.(2362)

"new Date(msg.date)" is marked with this one:
The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.(2363)

Any advice is appreciated on why this code is bad. Thanks in advance!

The syntax checker does not understand that if you perform arithmetic on a Date object then javascript will automatically call getTime() for you to convert the dates to milliseconds. If you explicitly do the conversion then it should be happy
let age = (new Date().getTime() - new Date(msg.date).getTime()) / (1000 * 60)

1 Like

Alternatively try

let age = (new Date().valueOf() - new Date(msg.date).valueOf()) / (1000 * 60);
1 Like

thank you guys! Both solutions work, I think :beers:

or

let age = (Date.now() - new Date(msg.date).getTime()) / (1000 * 60)
1 Like

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