Question from Node-Red newbie

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 :slight_smile:

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.

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

Depends on how human-readable you need it. :grinning:

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.

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

I do really want payload_values, zenofmud.

The code now looks like this:

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:

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

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

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.

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

@kuema - nice find, I was playing with the w3school's example and so confused because in their example, the toLocaleString worked! Must be using node v13.

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. :grin:

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

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