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
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
kuema
19 February 2020 10:54
2
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.
RalfPfitzner:
msg.payload_values
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.
kuema
19 February 2020 13:11
8
After a quick search I found that it appears to be an issue with NodeJS' locale support.
opened 05:18PM - 12 Sep 16 UTC
closed 06:16PM - 12 Sep 16 UTC
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...
intl
@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.
kuema
19 February 2020 13:30
10
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.
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
system
Closed
4 March 2020 13:31
12
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.