Convert OpenWeather epoch time

From OpenWeatherMap I get the sunrise ("payload.sunrise") but it is in epoch format (see image). I am unable to program the function node to display the time in human readable format and I have also not been able to do so with the information on this forum.

Can someone help me with the correct code for that function


node?

Hi @HaVi

Using a change node, you can format out the dates for your objects using JSONata.

$fromMillis(payload.sunrise*1000,'[Y0001]-[M01]-[D01] [H01]:[m01]:00')

Add a line for each of your date timestamps in the change node

Or if you prefer the Function node approch.

let d = new Date(0);
d.setUTCSeconds(msg.payload.sunrise);
msg.payload.sunrise = d
2 Likes

Thanks for helping Marcus, I chose the function node and it works perfectly!

I use this: node-red-contrib-moment (node) - Node-RED

2 Likes

I also wanted to chime in with the moment node. It's one of the first nodes I install on every new install of node-red, if not the first.

I did once use moment, but it can only process 1 property at a time (unless I'm mistaken)
So if you have multiple in 1 payload, it will become cumbersome to use.

The op seems to have 2, using the change node is much easier for situations like this.
Or maybe @TotallyInformation can develop a new one, that can process multiple properties using the more modern lib Day.js :wink:

i did not know this. I have only tried it one one value at a time

Nice to know! Though I'll be the first to admit that it hasn't had any TLC in a long time. Gets the job done though. Really needs replacing with something that uses a more current library since Moment isn't being developed any more. I just don't have time to do it.

Yes, it does, though I guess you could run your data through more than one :grin:

True.

I would love to have the time. Sadly, I don't. Still if someone did have the time, I'd be more than happy to let them loose on the repo and I'd even help :wink: Probably best to have a new node in the package so that people weren't disrupted by the change.

1 Like

Also a nice node to use, thanks for pointing it out. It's good to see how much support there is on the forum!

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