How to sum hours

How can i sum in one hour the msg.payload["2"]: "13:00". Like if 1: "13:00", 2 must be "14:00"Captura%20de%20tela%20de%202018-08-17%2013-54-26

I'm a little confused by your description... Is your goal to end up with this?

0: "Segunda"
1: "13:00"
2: "14:00"
3: "Leonardo"
4: "2018-08-20"

Yes, but its a generic case. I want to sum in one hour my 1 and put in the 2

Without seeing the code your using and the flow for context, the best I can say is to build a nasty looking javascript IF function that looks at the value of payload object 1 and uses it to decide what to place in payload object 2, then return the message. This would be terribly non-generic though, as each [1] time would have to be manually programmed with a [2] time.

https://www.w3schools.com/js/js_if_else.asp

Anyone else got ideas?

You can do simple time and date calculations with node-red-contrib-moment. This has the advantage of being able to deal with the complexities of summertimes, timezones, leapyears and other date oddities.

You can also use JSONata though I think you have to have the input in ISO format then convert to milliseconds. Perhaps not so good.

If you only want hours and can drop the minutes, then just use the time as a string, split on ":" and take the first element of the result as an integer.

Are the times you have actually strings or are they times? If times then you can just add a number to add one hour. If they are javascript times they are in milliseconds so you can add an hour by adding 3600000. Then when that is converted to a string for display it will show an hour later.

I dont know if is a string or time but when i sum 13:00 + 1 the result is 13:001.

What happens at "23:00" -- the next one should be "00:00"?

I would try using Julian's suggestion and install node-red-contrib-moment... use a change node to pull out element 1, use the moment node to convert to a datetime and offset by 1 hour, then use another change node to put the time back into your payload element 2.

Alternately, you could use some primitive JS code, like so (not fully tested):

var ts = "1970-01-01 " + msg.payload[1];  // build an epoch datetime
var ms = Date.parse(ts)+3600000;  // add 1 hour in milliseconds
var tm = new Date(ms).toTimeString();  // convert to time string

msg.payload[2] = tm.substring(0,5);
return msg;
1 Like

Where are you getting the times from?

Well, my project will start at 8:00 and finish at 19:00.

I made a integration with watson assistant, so the time e get from there. My msg.payload contains intents,the time is one of this intents and came like this: "13:00"

I think this will help. The arrow from up its my msg.payload before the function with name verifica horário/data/colaborador. I do the treatment of my variables in the lines 23 and 31, and the arrow bellow its my msg.payload after the function.

This is how i got the expected outcome(line 31)