How to print timestamp with full digits e.g. 12:02:05.54 and not 12:2:5.54

Hello. It is extremely important for the purpose of my project that I pass the timestamp as the output of a function node, it does not matter if it is Javascript or Python3

Right now I am using:

var today=nesw Date();
var date = today.getUTCFullYear()+'-'(rotday.getUTCMonth()+1)+'-'+today.getUTCDate()+'T'+today.getUTCHours()+':'+today.getUTCMinutes()+':'+today.getUTCSeconds()+'.'+today.getUTCMilliseconds();
msg.payload=date
return msg;

The problem is that the date is sometimes outputted as 12:2:5.54 and I NEED 12:02:05.54 . As seen, in UTC.

Thanks!

I'm using node-red-contrib-moment to format dates and times.

Using the node-red-contrib-moment node is indeed the big hammer that I also use, but in this case, it looks like the OP is just trying to format the current timestamp as an ISO-8601 string.

That can be done with the built-in JS Date method, like so:

> new Date().toISOString();
'2019-07-03T14:46:32.794Z'

So if you don't want the trailing "Zulu" timezone, your function could just be:

msg.payload = new Date().toISOString().replace("Z", "");
return msg;