I got a function node which collects MQTT Status messages from my zigbee2mqtt Broker. In particular I want the last_seen time stamp to monitor my zigbee network.
when a new device is detected an array is extended like:
The javascript date constructor might work fine however in NR it is always(!) displayed and also exported as a string. You need to represent your date object as number of millisecond since 1970 thus:
time: Date.parse(msg.payload.last_seen)
This value can be inspected in the NR debug- and context data windows. And of course it allows date sorting functions to be added.
You need to read up a bit on how JavaScript objects are stringified. When a data object is sent to the debug panel or indeed to MQTT, it is automatically converted by JavaScript to an ISO8601 string which is always shown as Zulu time (AKA GMT, AKA UTC).
This is because the communication between JavaScript and HTML (the panel) or MQTT uses JSON, not JavaScript Objects. JSON does not support anything other than JavaScript primitives (number, string, boolean, etc).
"stringified" : nice word for the problem
I understand this needed for display, JSON export etc. But what about the internal NR variables in flows or global: do the store a date object or also only the stringified version?
It can get a little complex but in general, when using the default memory based context variables, there is no need for them to be stringified. However, file-based context and possibly others do have to be converted, these are physical limitations not Node-RED ones.
Anywhere that your data has to leave the node.js environment will generally result in a conversion of some sort. This is true for databases as well though they have richer data types to work with, including their own timestamps. So it is less obvious there.