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 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring

Once you have the string, you can use the parseInt function to convert to integers - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

that is not the ideal way of extracting date parts.

Check out the date object

example...

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

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