I'm using the following flow to read a Modbus power meter Flow 2, this is the string in the function node Function node. I would like to add a time format to the msg.payload.
Do you mean {"Time": 2020-03-04 12:15:10, "Voltage":228.16} in the payload?
Can I first ask why you want the time as a string rather than a javascript timestamp? Often it is best to keep it as a timestamp and only convert to string when needed for display.
That is very easy then, if you pass the message containing a payload of {"Voltage":228.16} to a function node containing
msg.payload.Time = new Date()
return msg;
then that will do it. That assumes you already have the voltage property already setup. If you just have that value as a number in the payload then you can put them both in using
msg.payload = {Time: new Date(), Voltage: msg.payload}
return msg;
so this will also will have problems, for the same reason:
{"Time": 2020-03-04 12:15:10, "Voltage":228.16}
Should be formatted in JSON as a string, to insure smooth sailing:
{"Time": "2020-03-04 12:15:10", "Voltage":228.16}
Sometimes when having problems (or just building JSON strings), it is a good idea to simply and quickly take your proposed JSON string and copy-and-paste it into some kind of visual editor which will format the JSON.
For example, I use Visual Studio Code, and when I have a JSON string when developing / programming, I quickly copy-and-paste the string into a VSC file with a .json extension and it either formats as a proper JSON string or it does not.
This will often save (us) a lot of time trying to get an API or JSON payload to work when there is some JSON formatting error.
How do you know? Does it not depend on where you are sending it? Sending a javascript date may be perfectly valid.
In addition if sending as a string it is often important to include the timezone information. {Time: new Date()}
does that implicitly.