# Change date/time format

**URL:** https://discourse.nodered.org/t/change-date-time-format/76286
**Category:** General
**Tags:** function-node, javascript
**Created:** [8 March 2023 10:22 UTC](https://discourse.nodered.org/t/change-date-time-format/76286 "2023-03-08T10:22:18Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![maru](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maru/32/99601_2.png) [@maru](https://discourse.nodered.org/u/maru)
#### Post date: [8 March 2023 10:22 UTC](https://discourse.nodered.org/t/change-date-time-format/76286/1 "2023-03-08T10:22:18Z")

</div>

Good morning all,

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

```auto
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:

```auto
[{"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

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [8 March 2023 11:59 UTC](https://discourse.nodered.org/t/change-date-time-format/76286/2 "2023-03-08T11:59:24Z")

</div>

> [@maru](#):
>
> `d.toLocaleDateString('en-US', options);`

What locale are you in?

> [@maru](#):
>
> 08-03-2023 10:38u

what does `u` mean?  
Is `u` always present?

If you do this...

```auto
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...

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

```

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

---

<div class="post-metadata">

### Author: ![maru](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maru/32/99601_2.png) [@maru](https://discourse.nodered.org/u/maru)
#### Post date: [8 March 2023 17:17 UTC](https://discourse.nodered.org/t/change-date-time-format/76286/3 "2023-03-08T17:17:49Z")

</div>

Steve, thank you for the reply 👍

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 😒

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

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [8 March 2023 17:34 UTC](https://discourse.nodered.org/t/change-date-time-format/76286/4 "2023-03-08T17:34:44Z")

</div>

> [@maru](#):
>
> this has happened before

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.

---

<div class="post-metadata">

### Author: ![maru](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maru/32/99601_2.png) [@maru](https://discourse.nodered.org/u/maru)
#### Post date: [8 March 2023 19:20 UTC](https://discourse.nodered.org/t/change-date-time-format/76286/5 "2023-03-08T19:20:20Z")

</div>

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? 🤔  
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. 😅

---

<div class="post-metadata">

### Author: ![maru](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maru/32/99601_2.png) [@maru](https://discourse.nodered.org/u/maru)
#### Post date: [11 March 2023 17:15 UTC](https://discourse.nodered.org/t/change-date-time-format/76286/6 "2023-03-11T17:15:52Z")

</div>

> [@Steve-Mcl](#):
>
> $moment

Hi Steve,

Thanks for your ideas,

> [@Steve-Mcl](#):
>
> you will get this: `'2023-03-08 11:41'`

I think this is fine for now 👍

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [10 May 2023 17:16 UTC](https://discourse.nodered.org/t/change-date-time-format/76286/7 "2023-05-10T17:16:01Z")

</div>

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