What is the JS function in the debug windows for date conversions?

I have been fiddling with this for hours now (yes, really) ... I am new at this so I need to put in some research time before I run to the forums.

In the node-red debug pane I can output a timestamp as a numeric entry:
1587723989
When I click on that hyper-linked value I get the nice little ISO date/time:
2020-04-24T10:26:29.000Z

I am outputting this to MQTT so I want to insert a function node to do the exact same function that clicking in the debug window is doing.

var d = new Date();
// the question is what goes here to populate the d variable to perform the same conversion as the debug pane
var n = d.toISOString();

I tried the "moment" and "simpletime" nodes and never got them working properly, plus would prefer to know how to code this simple conversion in a function node.

Thanks for any guidance! I have just been coding in node-red a couple days.

Where do you get the timestamp from ? (meaning input)

try d.toLocaleString ()

good info here and here

The numbers come from a REST API call. Does that matter to the debug window? It just is converting the msg.payload number, right?

The formatting of the output will probably not be an issue, it is the population of the date/time variable using the number. What I was looking for was something like:

var d new Date();
d.populateDateFromEpochNumber(myNumber); <- this is the magic function I search for :slight_smile:

var d = new Date(epoch_number)

Steve,
I knew I had tried that before but the string I got from the following code is not the correct date

var d = new Date(msg.payload); // payload has 1587732484
var n = d.toISOString();
msg.payload = n;
return msg; // msg.payload now has "1970-01-19T09:02:12.484Z"

for the number: 1587732484 shown in the debugger pane...
clicking on the hyperlinked number shows: (the correct value)
2020-04-24T12:48:04.000Z

I assume I am missing something simple here.

Javascript uses milliseconds internally, UNIX timestamps are usually in seconds

var d = new Date(1587732484 * 1000)

That was the whole problem, Steve. Thanks much for your insight. It is a lesson I won't ever forget!

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