Cut Strings and convert to int

Hey guys,

i get some Sensor Values per MQTT in my Node Red Flow and they always have a timestamp which always has the same form.
The Code should look like this. But i don't know how to get Characters out of the whole string by their position index and then convert them to integer, because i need to send them per Modbus-TCP.

//Nachricht: 2020-09-11T10:03:50Z

var StringToCut = msg.payload.datetime;

var Monat = StringToCut.getCharAt(5-6);
Monat = Monat.parsetoIn

var Jahr = StringToCut.getCharAt(0-3);;
var Tag;
var Stunde;
var Sekunde;
var Minute;

return msg;

Thanks in Advance!

Hi @SalazarSlytherin

have a look at the String.substring function -

Once you have the string, you can use the parseInt function to convert to integers -

that is not the ideal way of extracting date parts.

Check out the date object


var d = new Date(msg.payload.datetime);
msg.payload.Monat = (d.getMonth()+1)
msg.payload.Jahr = d.getFullYear();
msg.payload.Tag; //etc 
return msg;

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