Node-red calendar timestamp formatting

Hi there!!
I’m pulling some calendar events from Nextcloud to shove it into FarmOS and also display it on the Node-Red dashboard.

I managed to get the events into Node-Red, but i’m struggling with the formatting of the DTSTAMP of the event to a readable format on the dashbard.
The msg object is stripped down to only contain event SUMMARY and DTSTAMP.

bilde

With a switch node I manage to run a JSONata to format it if it’s a single timestamp. But I can’t figure it out when it’s a part of the msg.payload array.

@Farmer-Ed, did you run into this problem?

Ideally I would format the date in this function node.

 msg.options = [];
var name, eventdate;
for (var i = 0; i < msg.payload.data.length; i++) {
    name      = msg.payload.data[i].SUMMARY;
    eventdate = msg.payload.data[i].DTSTAMP;
    msg.options[i] = {name,eventdate};
    msg.options[i].eventdate = eventdate;
    msg.options[i].name = name;
}
msg.payload = msg.options
return msg;

I also found a node to format the date, but I cant figure out how to set the input and output right when its an array.


This node also allows to set the output format to terms like “tomorrow”, “in 2 days” etc.

This happens to me sometimes… I’m stuck, ask for help, and then suddenly find the solution myself :slight_smile:

I added a split-node. This node splits the array, so I can run the format the date-node on each element in the array. Then I joined it back to an array, with a more readable date format

bilde

1 Like

To reformat the date for farmOS in a function node, something like this, I think should split up the date and join it back together with the hyphens (and add a preceding 0 of day or month is a single digit) .

function padZero(i) {
    return i < 10 ? "0"+i : i;
}
date = new Date(eventdate);
const farmosdate = date.getFullYear()+'-' + (padZero(date.getMonth()+1)) + '-'+padZero(date.getDate());
1 Like

That’s what I was looking for in the first place. Less nodes this way, and more JS-learning :wink:

1 Like

Dates can be messy between different API’s and I’ve had to reformat them in a few ways, but I think that’s the version that suits your situation.

Well done on finding another solution anyway, if we just wrote everything in a function node with pure JavaScript all the time then you’d need less nodes but also defeat the purpose of Node-Red, the beauty of it is being able to mix code with no code/low code. Nothing wrong with either approach though.