'How to write a function in Node-red for format Time

How to write a function in Node-red for format Time?

I have written something like this-:

msg.payload = new Date().toLocaleTimeString()  

but the output is showing up HH:MM:SS AM or PM. I need to drop AM/PM from the end. Please suggest how to get output in HHMMSS format.



Solution 1:[1]

The easiest way is probably to use something like the node-red-contrib-date node or the node-red-contrib-moment node rather than to try and do it in a function node.

If you need to use a function node then you will need to use a 3rd party npm module like dateformat. To use this module you will need to import it into the functionGlobalContext as described in the docs here

Solution 2:[2]

You can use JSONata expression

$now('[Y0001]-[M01]-[D01] [H01]:[m01]','+0300')

Last argument is Timezone shift

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hardillb
Solution 2 user15840060