# Question from Node-Red newbie

**URL:** <https://discourse.nodered.org/t/question-from-node-red-newbie/21970>\
**Category:** General\
**Created:** [19 February 2020 10:44 UTC](https://discourse.nodered.org/t/question-from-node-red-newbie/21970 "2020-02-19T10:44:48Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![RalfPfitzner](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@RalfPfitzner](https://discourse.nodered.org/u/RalfPfitzner)\
**Post date:** [19 February 2020 10:44 UTC](https://discourse.nodered.org/t/question-from-node-red-newbie/21970/1 "2020-02-19T10:44:48Z")

</div>

Hi folks

In node-red I have an array of unix epoch timestamps which I would like to convert to human friendly time stamps.

Here is my function node which is NOT working 🙂

```auto
var i;
var attributes_values = [];
var attributes_timestamps = [];
var attributes_timestamps_human = [];
for (i=0; i < msg.data.attributes.data.length; i++) {
    attributes_values[i] = msg.data.attributes.data[i].marketprice /10 * 1.19;
    attributes_timestamps[i] = msg.data.attributes.data[i].start_timestamp / 1000;
    attributes_timestamps_human[i] = attributes_timestamps[i].toUTCString();
}
msg.payload_values = attributes_values;
msg.payload_timestamps = attributes_timestamps;
msg.payload_timestamps_human = attributes_timestamps_human;
return msg;

```

Here is an extract of the original data set which I want to manipulate.

```auto
attributes: object
data: array[13]
[0 … 9]
0: object
start_timestamp: 1582106400000
end_timestamp: 1582110000000
marketprice: 34
unit: "Eur/MWh"

```

So, any help of a wise guy/girl is highly appreciated to get the attributes\_timestamps\_human up and running.

Thanks upfront  
Ralf

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [19 February 2020 10:54 UTC](https://discourse.nodered.org/t/question-from-node-red-newbie/21970/2 "2020-02-19T10:54:13Z")

</div>

Depends on how human-readable you need it. 😀

Easiest would be using the Date constructor with your millisecond timestamp, like `new Date(timestampMs)`

Then use the `toISOString()` method to get an ISO 8601 string.

> **[Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)**
>
> JavaScript Date objects represent a single moment in time in a platform-independent format.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [19 February 2020 10:58 UTC](https://discourse.nodered.org/t/question-from-node-red-newbie/21970/3 "2020-02-19T10:58:33Z")

</div>

> [@RalfPfitzner](#):
>
> msg.payload\_values

do you really want `msg.payload_values` or do you want `msg.payload.values` (same for the timestamp outputs)?

---

<div class="post-metadata">

**Author:** ![RalfPfitzner](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@RalfPfitzner](https://discourse.nodered.org/u/RalfPfitzner)\
**Post date:** [19 February 2020 11:28 UTC](https://discourse.nodered.org/t/question-from-node-red-newbie/21970/4 "2020-02-19T11:28:40Z")

</div>

I do really want payload\_values, zenofmud.

---

<div class="post-metadata">

**Author:** ![RalfPfitzner](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@RalfPfitzner](https://discourse.nodered.org/u/RalfPfitzner)\
**Post date:** [19 February 2020 11:41 UTC](https://discourse.nodered.org/t/question-from-node-red-newbie/21970/5 "2020-02-19T11:41:05Z")

</div>

The code now looks like this:

```auto
var i;
var attributes_values = [];
var attributes_timestamps = [];
var attributes_timestamps_human = [];
for (i=0; i < msg.data.attributes.data.length; i++) {
    attributes_values[i] = msg.data.attributes.data[i].marketprice /10 * 1.19;
    attributes_timestamps[i] = msg.data.attributes.data[i].start_timestamp;
    attributes_timestamps_human[i] = new Date(attributes_timestamps[i]).toLocaleString('de-DE', { hour12: false });
}
msg.payload_values = attributes_values;
msg.payload_timestamps = attributes_timestamps;
msg.payload_timestamps_human = attributes_timestamps_human;
return msg;

```

The output is unfortuantely like this:

```auto
payload_timestamps_human: array[12]
[0 … 9]
0: "2/19/2020, 12:00:00"
1: "2/19/2020, 13:00:00"
2: "2/19/2020, 14:00:00"
3: "2/19/2020, 15:00:00"

```

So this is not the format like dd.mm.yyyy, which I would like to have.  
The hours in 24h format is as expected.

What's going wrong??

Thanks  
Ralf

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [19 February 2020 12:13 UTC](https://discourse.nodered.org/t/question-from-node-red-newbie/21970/6 "2020-02-19T12:13:57Z")

</div>

If you want a leading zero of the month, try a google search 'jasascript date with leading 0 on month' and you will find lots of suggestions/answers

---

<div class="post-metadata">

**Author:** ![RalfPfitzner](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@RalfPfitzner](https://discourse.nodered.org/u/RalfPfitzner)\
**Post date:** [19 February 2020 13:01 UTC](https://discourse.nodered.org/t/question-from-node-red-newbie/21970/7 "2020-02-19T13:01:17Z")

</div>

It's not the leading 0, zenofmud. It's the formating in "German" standard".

I thought, the `toLocaleString('de-DE', { hour12: false })` would do that.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [19 February 2020 13:11 UTC](https://discourse.nodered.org/t/question-from-node-red-newbie/21970/8 "2020-02-19T13:11:15Z")

</div>

After a quick search I found that it appears to be an issue with NodeJS' locale support.

> <https://github.com/nodejs/node/issues/8500>
>
> In all three of my environments I was able to reproduce this error:
> My production server
> Version: v6.5.0
> Platform: Linux 2.srv.wilkynet.uk 4.4.11-x1-64+ #1 SMP...

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [19 February 2020 13:26 UTC](https://discourse.nodered.org/t/question-from-node-red-newbie/21970/9 "2020-02-19T13:26:53Z")

</div>

@kuema - nice find, I was playing with the w3school's [example](https://www.w3schools.com/jsref/jsref_tolocalestring.asp) and so confused because in their example, the toLocaleString worked! Must be using node v13.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [19 February 2020 13:30 UTC](https://discourse.nodered.org/t/question-from-node-red-newbie/21970/10 "2020-02-19T13:30:04Z")

</div>

That's the thing, it's working correctly in the browser.

But I remember that I stumbled upon this problem with NodeJS in the past. It was a problem with timezones, so I suspected something similar. 😁

---

<div class="post-metadata">

**Author:** ![RalfPfitzner](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@RalfPfitzner](https://discourse.nodered.org/u/RalfPfitzner)\
**Post date:** [19 February 2020 13:31 UTC](https://discourse.nodered.org/t/question-from-node-red-newbie/21970/11 "2020-02-19T13:31:50Z")

</div>

Hmm, even with installing the full-icu package as mentioned in the link from @kuema I get the same result.

Running Node-Red 1.0.3 on [hass.io](http://hass.io)

---

<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:** [4 March 2020 13:31 UTC](https://discourse.nodered.org/t/question-from-node-red-newbie/21970/12 "2020-03-04T13:31:51Z")

</div>

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