Change date/time format

Good morning all,

I use this function node in my flow to check when my device sent the last data.

var d = new Date();
var options = {year: 'numeric', month: '2-digit', day: '2-digit', hour: `2-digit`, minute: `2-digit`, second: `2-digit`};
var _resultDate = d.toLocaleDateString('en-US', options);
msg.payload = _resultDate;
return msg;

The output is in this format:
03/08/2023, 10:38:19 AM

Is it possible to adapt this function to this format:
08-03-2023 10:38u

TestFlow:

[{"id":"5f343f10e8cf0863","type":"debug","z":"bbde0b5a14e0d0bc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2090,"y":120,"wires":[]},{"id":"ff71cffbbb0b2b46","type":"function","z":"bbde0b5a14e0d0bc","name":"Format Time","func":"var d = new Date();\nvar options = {year: 'numeric', month: '2-digit', day: '2-digit', hour: `2-digit`, minute: `2-digit`, second: `2-digit`};\nvar _resultDate = d.toLocaleDateString('en-US', options);\nmsg.payload = _resultDate;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1910,"y":120,"wires":[["5f343f10e8cf0863"]]},{"id":"e0e7d150ac885167","type":"inject","z":"bbde0b5a14e0d0bc","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":1750,"y":120,"wires":[["ff71cffbbb0b2b46"]]}]

Kind regards,
Marco

What locale are you in?

what does u mean?
Is u always present?

If you do this...

const d = new Date()
msg.payload =  new Intl.DateTimeFormat('en-GB', { dateStyle: 'short', timeStyle: 'short'}).format(d)
return msg

you will get this: '08/03/2023, 22:41' (you could then replace the / with - and add a u)?


Advice:
to avoid confusion, the ISO standard date format of YYYY-MM-DD is always my 1st choice as it is both "sortable" and difficult to misinterpret (e.g. 2000-3-4 is ALWAYS "4th of March" whereas 2-1-2000 is "1st of Feb" in the US but is "2nd of Jan" everywhere* else)

You can get that format using a change node and $moment or in a function like this...

msg.payload = new Date().toISOString().substring(0, 16).replace('T', ' ')
return msg

you will get this: '2023-03-08 11:41'

Steve, thank you for the reply :+1:

Unfortunately, can't test this now. I had to power off my rpi and now I lost a few flows and I can't deploy :unamused:

I will first try to bring the old situation back. If I remember well, this has happened before :thinking: ....

Are you using good quality SD cards from reputable suppliers? Did you do a proper shutdown of the pi or pull the plug? Even pulling the plug will only very rarely corrupt a card.

Hi Colin,

No, I am back again, after importing an "all flow's" backup which made just before I pulled the plug out.

I stared at a tab with 1 flow for 1 device while I was sure that there had to be 4 flows? :thinking:
Then I discovered scrolling through the tabs, the complete tab with the missing flows and more confusion, this tab had the same name as the one I stared at. :sweat_smile:

Hi Steve,

Thanks for your ideas,

I think this is fine for now :+1:

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.