Msg object is not readable info

Good day people of Node-Red

I have a serious problem with the info I`m reading of my weather API.

I get info into a function node that exports the sunset and sunrise times, now the problem is that it gives me the info in a weird way.

EG. Object:

sunrise: 1572060152

sunset: 1572106754

now if I click on the number it changes to a HEX. And if I click on it again it changes to something else, not sure what u call it but it looks like this "2019-10-26T03:22:32.000Z" And then if I click it again I get the info that I require 10/26/2019, 5:32:32 AM [UTC+2]

I need to get the time extracted from that info but it doesn't matter what I try the outcome is always the 10 digit number...

Please help.

That looks like UNIX (or Epoch) time. When you click on it in the debug panel, NR changes it to a more readable format for you (those developers are pretty cool!)

You could feed that into the moment node (node-red-contrib-moment and it willl handle it for you.

alternative as you are already using a function node you can multiply the unixtimestamp by 1000 to get a javascript timestamp and then use the javascript date commands

1 Like

image

image

As you can see I tried the moment's node... And it changed it to 1970???

What code do i insert in the funtion node.
I tried moments(string).

Ive been on this for 2 days?? No Luck

Here are the docs for the standard JavaScript Date object:

It provides a number of functions to generate a human readable string. For example:

msg.sunrise = (new Date(msg.sunrise)).toISOString();
1 Like

Hi knolleary,

I tried your method but the dates are returned in the 1970`s...
Here is my function node code:

srise = (new Date(msg.payload.sunrise)).toISOString();
sset = (new Date(msg.payload.sunset)).toISOString();

return {sunrise : srise, sunset : sset};

My payload looks like this:

10/27/2019, 7:47:48 PMnode: ea77ae2.594fe5
msg : Object
object
sunrise: "1970-01-19T04:43:52.921Z"
sunset: "1970-01-19T04:44:39.716Z"
_msgid: "81f13ea7.e6e22"

Thank you for the help so far...

Try multiplying by 1000

srise = (new Date(msg.payload.sunrise * 1000)).toISOString();

etc

Hi dceejay,

Ok that fixed the date
Here is my function node code:

srise = (new Date(msg.payload.sunrise * 1000)).toISOString();
sset = (new Date(msg.payload.sunset * 1000)).toISOString();

return {sunrise : srise, sunset : sset};

Here is the payload:

10/27/2019, 7:56:54 PMnode: ea77ae2.594fe5
msg : Object
object
sunrise: "2019-10-28T03:22:01.000Z"
sunset: "2019-10-28T16:21:56.000Z"
_msgid: "2589830c.3f607c"

how to fix the time??

If you mean that you want it in your local timezone, the answer is in the page that @knolleary linked to

What do you mean by 'fix'…? What do you want it to look like?

Hi knolleary,

I managed to fix the time somewhat....
My time now looks like this.:

10/27/2019, 9:06:47 PMnode: ea77ae2.594fe5
msg : Object
object
sunrisetime: "Monday, October 28, 2019, 3:22:01 AM"
sunsettime: "Monday, October 28, 2019, 4:21:56 PM"
_msgid: "cf9c2bc9.a671c8"

But it is in the wrong timezone...
it is in UTC if I read correctly on the page that you said i must visit.
But I can't seem to set the timezone to UTC+2.

Here is my Function Node so far...

var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };

srisetime = (new Date(msg.payload.sunrise * 1000)).toLocaleTimeString('en-ZA');
ssettime = (new Date(msg.payload.sunset * 1000)).toLocaleTimeString('en-ZA');


return {sunrisetime : srisetime, sunsettime : ssettime};

Im trying to find a solution... But on that page its like finding a needle in a bunch of needles. :smiley:

look at the “ toLocale” parts of that page

1 Like

Thank you ukmoose...
It worked 100% almost 3 days struggling with this.
Thanks for pushing me to read the documentation.

Here is the Final Function Node:

srisetime = ((new Date(msg.payload.sunrise * 1000)).toLocaleString('en-ZA', { timeZone: 'Africa/Johannesburg' }));
ssettime = ((new Date(msg.payload.sunset * 1000)).toLocaleString('en-ZA', { timeZone: 'Africa/Johannesburg' }));


return {sunrisetime : srisetime, sunsettime : ssettime};

And here is the end Result:

10/27/2019, 9:37:52 PMnode: ea77ae2.594fe5
msg : Object
object
sunrisetime: "10/28/2019, 5:22:01 AM"
sunsettime: "10/28/2019, 6:21:56 PM"
_msgid: "67922e46.308ef"

Once again thank you!!

1 Like