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

i use this node to display endTime of the sun in Dashboard : node-red-contrib-sunpos
Run on a RPI, This is the result :
AMPM

  • on top : the endTime in US format
  • on bottom (for information) : the time in a chart in FR format
[{"id":"451fc132.9331d","type":"inject","z":"fe2c7bc6.bd49c8","name":"make request","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":"","x":96.04759216308594,"y":2122.65452003479,"wires":[["3df695cb.25097a"]]},{"id":"3df695cb.25097a","type":"sunpos","z":"fe2c7bc6.bd49c8","name":"","lon":"6.4752","lat":"43.5427","start":"sunrise","startoffset":0,"end":"sunset","endoffset":0,"x":250,"y":2120,"wires":[["796a91ee.2919c","2147b18.79c844e"]]},{"id":"796a91ee.2919c","type":"function","z":"fe2c7bc6.bd49c8","name":"endTime +alt/azi+icone","func":"var seconds = msg.payload.endTime;\nvar endLocaleTime;\nvar azimuth = Math.round(msg.payload.azimuth*10)/10;//msg.payload.azimuth;//azimuth = azimuth.toFixed(1);\nvar altitude = Math.round(msg.payload.altitude*10)/10;//msg.payload.altitude;//altitude = altitude.toFixed(1);\nvar sunInSky = msg.payload.sunInSky;\n\n//transforme epoch en 21:01:449\nvar d = new Date(msg.payload.endTime);\nvar n = d.toLocaleTimeString();\nendLocaleTime = n;\n\n//compare si nuit ou jour ?\nvar secondsNow = msg.time;\nif (secondsNow > seconds || secondsNow < msg.startTime){\n    flow.set('wvcPow5min',0 );  //evite 0.8w toute la nuit\n    msg.razCountEnergy = 0;\n    msg.state = \"wvcPow5min=0 !\";\n    msg.icone = 'https://cdn.icon-icons.com/icons2/1370/PNG/128/if-weather-4-2682847_90799.png';//https://cdn.icon-icons.com/icons2/799/PNG/128/night-01_icon-icons.com_65776.png';//https://cdn.icon-icons.com/icons2/1465/PNG/512/640crescentmoon_100402.png';\n    msg.title = 'Stand by !' ;\n    \n}else{\n    msg.razCountEnergy = 0;\n    msg.state = \"Production !\";\n    msg.icone = 'https://cdn.icon-icons.com/icons2/1370/PNG/128/if-weather-3-2682848_90785.png';//https://cdn.icon-icons.com/icons2/799/PNG/128/sunny512x512-01_icon-icons.com_65775.png';//https://cdn.icon-icons.com/icons2/1465/PNG/512/643sunwithface_101008.png';\n    msg.title = 'Working good !' ;\n   \n}if (secondsNow == seconds){\n    msg.razCountEnergy = 1;\n    msg.state = \"Production en pause !\";\n    }\n\n//********** affiche le status du Node **********\nnode.status({text:n}); //value});\n\n//prepare les sorties\n node.send([{\n    startTime:msg.startTime,\n    endTime:msg.endTime,\n    endLocaleTime:endLocaleTime,\n    seconds:seconds,\n    azimuth:azimuth,\n    altitude:altitude\n    \n },null,null]);\n \n node.send([null,{\n     state:msg.state,\n     icone:msg.icone,\n     title:msg.title,\n     razCountEnergy:msg.razCountEnergy,\n     sunInSky:sunInSky\n     \n },null]);\n \n  node.send([null,null,{\n    payload:altitude\n     \n }]);\n /*\n msg.payload = [];//prepare les sorties\n node.send([{\n    startTime:msg.startTime,\n    endTime:msg.endTime,\n    endLocaleTime:endLocaleTime,\n    seconds:seconds,\n    azimuth:azimuth,\n    altitude:altitude\n    \n },null]);\n \n node.send([null,{\n     state:msg.state,\n     icone:msg.icone,\n     title:msg.title,\n     razCountEnergy:msg.razCountEnergy,\n     sunInSky:sunInSky\n     \n }]);\n */\n \n \n \n /*\n msg.payload = [];//prepare les sorties\n node.send([{\n    startTime:msg.startTime,\n    endTime:msg.endTime,\n    endLocaleTime:endLocaleTime,\n    seconds:seconds,\n    azimuth:azimuth,\n    altitude:altitude\n    \n },null]);\n \n node.send([null,{\n     payload:msg.endTimeOn\n     \n }]);\n */","outputs":3,"noerr":0,"x":462,"y":2113,"wires":[["3df592e4.ea359e","40a39504.ff86ec","f69ba939.ed66c8","557aeff4.a36cb","32bbb7dd.b9f828"],["9a43d3a.7772d3","d5355045.f137b"],["109fee9.3012011"]]},{"id":"40a39504.ff86ec","type":"ui_text","z":"fe2c7bc6.bd49c8","group":"8aeba035.813e","order":6,"width":"4","height":"1","name":"endTime","label":"<font color=grey>{{seconds}}s","format":"{{endLocaleTime}}","layout":"row-left","x":904.9406318664551,"y":2101.2142848968506,"wires":[]},{"id":"8aeba035.813e","type":"ui_group","z":"","name":"Power","tab":"255f2630.e1c57a","order":1,"disp":true,"width":"12","collapse":true},{"id":"255f2630.e1c57a","type":"ui_tab","z":"","name":"wvc","icon":"flash_on","order":2,"disabled":false,"hidden":false}]

This is the local variables in RPI :
image

My question : before few days the endTime is displayed in FR format while until now he was in US. Why display in US now ?

Are you running the browser on a different machine to node-red? If so check the time zone of that machine is also FR. Also what does the date command show on the Pi, and what time was it when you ran the date command?

i used browser in both : chromium in the RASPBIAN where node Red run, and with chrome on a WIN10 connected on http://192.168.1.54:1880/
The both display endTime in US . All other times in FR.

This is the result datecommande :
image

What does the sunpos node show in its status display next to the node in the editor when you click the Inject node?
[Edit] and what is shown for the status of your function node?
Also, I assumed that you meant it was showing the wrong time zone, are you actually saying the problem is that it is showing am/pm rather than the 24 hour clock?

image

Some days ago, it displayed correctely FR :
image

Meanwhile, it seems to me, I do not remember too much, that I made an update of RASPBIAN. I am unable to say if the problem came at the same time. But we see that today local RPI are all in FR, and the date is well in 24h!

Exactely ! I want it showing 24 hours clock like before.

OK, there is nothing particularly American about using a 24 hour clock as opposed to am/pm.

How the browser decides which format to use I don't know. Mine shows am/pm. If you want to force it to a particular format you could use node-red-contrib-moment which I believe will let you format it however you like.

what is annoying is that AM / PM replaced 24 hours without me modifying anything. It annoys me to add a function to a node that worked very well.
I asked the author of this node who does not understand either.
It's a mystery ! :thinking:

Though not with that exact problem, I get from where you are coming.

I have had problems like that happen to me many times.

Alas I have found it is not the best to worry about it.
Add the extra node and move on.
I know it is frustrating, but sometimes it just isn't worth the effort.

One day in the future, you may get it to what went wrong and all you then need to do is remove that extra node.

Out of curiosity, I loaded your code and this is what I get:
Screenshot%20from%202019-06-09%2008-00-13

Which to me looks like 24 hour format. US format as you call it.

So to me it is pointing towards a local setting in/on your machine somewhere.
I may be wrong - and it won't be the last - but I hope it helps you somewhere/how in the scheme of things.

1 Like

In your function you are using toLocaleTimeString() which is supposed to use information from the locale to format it. Therefore I deduce that either the implementation of that function has changed (so a change in nodejs) or something in your locale has changed.
Searching I found several references to this function being somewhat problematic, so possibly it is best to avoid it.
I see that the sunpos node is also using this function for its status display so that explains why that shows the same as you are seeing.

I think your are not wrong , and probaby after an update of RASPBIAN ? node.js ? etc ...

it reassures me that you display the format 24h, it necessarily come from the machine. I will try again update we'll see.

@Trying_to_learn what version of node are you running on that machine, which is showing it in 24 hour mode (not am/pm)?
node -v will show you .

@Colin : good question
mine is : image wait for @Trying_to_learn ?

Mine is 10.16.0 too and shows am/pm as yours does. I have just tried it on a node 8.x machine and it shows 24hr, however there are other differences so I can't be certain that is the cause. You could try downgrading to 8.x if you wanted to, but that would not be a long term solution, so probably better to use the moment node and then you can make it do exactly what you want. They might fix have fixed it in 12 of course, but there are nodes that are not yet compatible with 12 so that is not a good solution either.

That is on a ubuntu.

me@me-desktop:~$ node- v
Command 'node-' not found, did you mean:

  command 'node' from deb nodejs

Try: sudo apt install <deb name>

me@me-desktop:~$ 

The only meaning for AM is an old radio "format", compared to FM.

And PM is short for Private Message.

On one of my RPIs:

pi@TelePi:~ $ node -v
v8.12.0
pi@TelePi:~ $ 

But it is meaningless as that isn't the machine I showed you the result.

node -v not node- v With computers the devil is in the detail.
AM - ante meridiem, latin for before noon
PM - post meridiem, latin for after noon

Oops.

Sorry about the command.

Keys playing tricks on me.

Standby:
8.16.0

I guess the node- v is because I have been doing a lot of cd .node-red and so when I entered the node, I accidentally added the - straight after it.

Sorry.

I was kind of being ..... sarcastic (cheeky) about the AM/PM. Just I have been caught too many times with people and times. All my clocks/watches/computers are 24 hour time unless they can't do that.

I think the culprit is found. I'm not sure of the version I had before the update, but I'm pretty sure that after the update, it shows in AM / PM.
Is this an incompatibility of toLocaleTimeString () with version 10.16.0 ?
I hope for an update of node.js, to find my 24h.
Voluntarily, I will not add the Moment node to see if an update will put in order.

I found a solution: it avoids a Moment node

d.toLocaleTimeString ("fr-FR", {hour12: false});
image :smiley:

1 Like

That's worth remembering, thanks.

1 Like