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

**URL:** <https://discourse.nodered.org/t/what-is-the-js-function-in-the-debug-windows-for-date-conversions/25437>\
**Category:** General\
**Created:** [24 April 2020 17:54 UTC](https://discourse.nodered.org/t/what-is-the-js-function-in-the-debug-windows-for-date-conversions/25437 "2020-04-24T17:54:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Super-Nor](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@Super-Nor](https://discourse.nodered.org/u/Super-Nor)\
**Post date:** [24 April 2020 17:54 UTC](https://discourse.nodered.org/t/what-is-the-js-function-in-the-debug-windows-for-date-conversions/25437/1 "2020-04-24T17:54:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [24 April 2020 18:31 UTC](https://discourse.nodered.org/t/what-is-the-js-function-in-the-debug-windows-for-date-conversions/25437/2 "2020-04-24T18:31:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [24 April 2020 18:50 UTC](https://discourse.nodered.org/t/what-is-the-js-function-in-the-debug-windows-for-date-conversions/25437/3 "2020-04-24T18:50:29Z")

</div>

try `d.toLocaleString ()`

good info [here](https://css-tricks.com/everything-you-need-to-know-about-date-in-javascript/) and [here](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date)

---

<div class="post-metadata">

**Author:** ![Super-Nor](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@Super-Nor](https://discourse.nodered.org/u/Super-Nor)\
**Post date:** [24 April 2020 19:18 UTC](https://discourse.nodered.org/t/what-is-the-js-function-in-the-debug-windows-for-date-conversions/25437/4 "2020-04-24T19:18:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Super-Nor](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@Super-Nor](https://discourse.nodered.org/u/Super-Nor)\
**Post date:** [24 April 2020 19:22 UTC](https://discourse.nodered.org/t/what-is-the-js-function-in-the-debug-windows-for-date-conversions/25437/5 "2020-04-24T19:22:36Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [24 April 2020 19:33 UTC](https://discourse.nodered.org/t/what-is-the-js-function-in-the-debug-windows-for-date-conversions/25437/6 "2020-04-24T19:33:51Z")

</div>

```auto
var d = new Date(epoch_number)

```

---

<div class="post-metadata">

**Author:** ![Super-Nor](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@Super-Nor](https://discourse.nodered.org/u/Super-Nor)\
**Post date:** [24 April 2020 19:52 UTC](https://discourse.nodered.org/t/what-is-the-js-function-in-the-debug-windows-for-date-conversions/25437/7 "2020-04-24T19:52:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [24 April 2020 19:58 UTC](https://discourse.nodered.org/t/what-is-the-js-function-in-the-debug-windows-for-date-conversions/25437/8 "2020-04-24T19:58:21Z")

</div>

Javascript uses milliseconds internally, UNIX timestamps are usually in seconds

```auto
var d = new Date(1587732484 * 1000)

```

---

<div class="post-metadata">

**Author:** ![Super-Nor](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@Super-Nor](https://discourse.nodered.org/u/Super-Nor)\
**Post date:** [24 April 2020 20:03 UTC](https://discourse.nodered.org/t/what-is-the-js-function-in-the-debug-windows-for-date-conversions/25437/9 "2020-04-24T20:03:26Z")

</div>

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

---

<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:** [8 May 2020 20:03 UTC](https://discourse.nodered.org/t/what-is-the-js-function-in-the-debug-windows-for-date-conversions/25437/10 "2020-05-08T20:03:35Z")

</div>

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