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;

Hello Mr.shrickus your method is good but i like to plus 7 for hh:mm:ss time because my timezone is UTC+7 how to do that? Could you help me please.

Hi @TrySorn, you probably should open a new thread instead of responding on a thread over 1year old.

Does .toLocaleString() work for you?

image

PS, i hope this isnt for inserting into a database? (toLocaleString should really only be used in presentation (i.e. user facing / dashboard))

In Javascript, new Date() creates a Date object representing the current date/time in GMT. If you want to see it formatted as a string using your current timezone, there are several built-in functions you can use:

> var now = new Date();

> now
2020-12-10T20:47:16.849Z
> now.toISOString()
'2020-12-10T20:47:16.849Z'
> now.toLocaleString()
'2020-12-10 15:47:16'
> now.toString()
'Thu Dec 10 2020 15:47:16 GMT-0500 (Eastern Standard Time)'
> now.toDateString()
'Thu Dec 10 2020'
> now.toTimeString()
'15:47:16 GMT-0500 (Eastern Standard Time)'

And if you want more control over the display format, try using the node-red-contrib-moment node.