[SOLVED] Issue : display AM/PM time instead of 24hours

It's a snippet of JavaScript. You would use it in a function node.

E.g...

var d = new Date();
msg.payload = d.toLocaleTimeString ("fr-FR", {hour12: false});
return msg;

However, where/why do you want to do this? Dashboard? If it is for dashboard then you can use angular formatters

Thanks for the fast reply.
Yes! The X-axis of the line chart on dashboard is labeled in 12h format and I want to change this label to 24h format.

sorry for the late reply, did you find the solution to your problem?

I believe the formatting is up to how your OS is setup to show times, and possibly how the browser is setup. It is not determined by node-red.

1 Like

This is an old thread but the problem still exists: no 24h format! I updated NodeRed to 3.1.3 and Node.js to 18.19. I tried Windows with Firebird and MS Edge and macOS with Firebird and Safari and iPadOS with Safari: no 24h format. Timedatectl under Debian prompts 24h format and the correct local time zone (CET). Any idea?

Are you running the browser on the same Debian machine? If not what is the native date format on the machine running the browser?

Where exactly are you trying to get 24h format? Can you post a very simple flow that shows the problem?

Edit - or do you mean the x axis of the chart as in the previous posts?

No, browsers are running on different machines, local time zone is set to automatic and results in CET as is in the Debian machine running NodeRed.


I am missing the 24h in the status text of the node-red-contrib-knx-ultimate nodes. I contacted the developer who told me that the nodes are showing 24h in his setting and that this behavior was independent from the node.

Thank you

What timestamps do you see in the debug pane?

24h format!

BTW: Status text of node-red-contrib-telegrambot-home nodes are pm/am only, as well.

It is ok for me
image

Can you try a browser on a different device? Your phone for example?

As I wrote:

I wonder if it is something odd about your particular timezone in combination with those nodes. There is certainly something odd going on. Unfortunately I haven't got a free system that I can configure with your timezone.
Having looked briefly at the code it appears to be converting it to a string in the server (rather than in the browser, which is more usual). It is using Date.toLocaleTimeString() so my guess is that nodejs 18 has a problem with toLocaleTimeString() in your timezone.

What does this flow show?

[{"id":"86dc1563d60cbd4d","type":"inject","z":"997da33a0beedade","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":280,"y":1440,"wires":[["d897a08e9dc373d3"]]},{"id":"d897a08e9dc373d3","type":"function","z":"997da33a0beedade","name":"function 4","func":"msg.payload = new Date().toLocaleTimeString()\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":1440,"wires":[["361963660cc785f2"]]},{"id":"361963660cc785f2","type":"debug","z":"997da33a0beedade","name":"debug 2473","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":1440,"wires":[]}]

"11:18:19 PM" :pensive:

I think the developer of this node uses CET, too. And he told me that 24h format works with his setup. I will ask him about his node.js.
Concerning node.js: 16.x did not show 24h as well, here.

What region is he in?

If in the flow I posted you replace the line in the function node
msg.payload = new Date().toLocaleTimeString()
with
msg.payload = new Date().toLocaleTimeString("en-UK")
what do you see.

As far as I can see, toLocaleTimeString defaults to the system locale, which I guess is 'en-US' so it shows AM/PM. If I use (in the UK) "en-US" in the call then I see AM/PM. If I am right then that is the problem. On a server with en-US as the region then it will show AM/PM and there is not much you can do about it, other than change the region in the server to en-UK

1 Like

Ah, this helped me. Locale prompted LANG=C. After dpkg-reconfigure locales and changing the default to the desired local and a reboot everything is fine now. Thank you very much for your help!

1 Like