# Format date at 'YYYY:MM:DD HH:MM;SS'

**URL:** <https://discourse.nodered.org/t/format-date-at-yyyydd-hh-mm-ss/83130>\
**Category:** General\
**Tags:** docker\
**Created:** [23 November 2023 14:12 UTC](https://discourse.nodered.org/t/format-date-at-yyyydd-hh-mm-ss/83130 "2023-11-23T14:12:28Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jen](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@Jen](https://discourse.nodered.org/u/Jen)\
**Post date:** [23 November 2023 14:12 UTC](https://discourse.nodered.org/t/format-date-at-yyyydd-hh-mm-ss/83130/1 "2023-11-23T14:12:28Z")

</div>

Hello.  
How can you format the date in the logs? now I have it '23 Nov 2023 hh:mm:ss'  
it must have been so 'YYYY:MM:DD HH:MM;SS'  
Writing a function just adds the date again((

```auto
const currentDate = new Date();
const formattedDateTime = currentDate.toISOString().replace(/T/, ' ').replace(/\..+/, '');

const { loggingLevel } = msg;
delete msg.loggingLevel;

const messageToLog = `${formattedDateTime} - [info] [function:Log message to system console] ${JSON.stringify(msg)}`;

if (loggingLevel === 'error') {
  node.error(messageToLog);
  return;
}

node.log(messageToLog);

```

I work in a docker

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [23 November 2023 14:41 UTC](https://discourse.nodered.org/t/format-date-at-yyyydd-hh-mm-ss/83130/2 "2023-11-23T14:41:44Z")

</div>

If you want to get a formatted date, the easiest way is to use JSONata, in a change node for example, and use `$moment()` in your JSONata expression

change your `tz` accordingly

```auto
$moment().tz("Europe/London").format("YYYY:MM:DD HH:mm:ss")

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/4807dccbbb6199675f5c9a5376b5f4cfdcc78987.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [23 November 2023 15:27 UTC](https://discourse.nodered.org/t/format-date-at-yyyydd-hh-mm-ss/83130/3 "2023-11-23T15:27:07Z")

</div>

Just add to @marcus-j-davies info.  
You can use moments direct in the function node  
e,g

```auto
[{"id":"436fe97ebbedc332","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":1580,"wires":[["003f6421dba7fad7"]]},{"id":"003f6421dba7fad7","type":"function","z":"d1395164b4eec73e","name":"function 148","func":"\nreturn {payload: \nmoment.tz(\"Europe/London\").format(\"YYYY\")}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"moment","module":"moment"},{"var":"momentTimezone","module":"moment-timezone"}],"x":350,"y":1580,"wires":[["1add159ae1ed848e"]]},{"id":"1add159ae1ed848e","type":"debug","z":"d1395164b4eec73e","name":"debug 2456","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":530,"y":1580,"wires":[]}]

```

You have to add moment and moment-timezone to the setup tab  
and enable functionExternalModules in your setting file.  
[https://nodered.org/docs/user-guide/writing-functions#using-the-functionexternalmodules-option](https://nodered.org/docs/user-guide/writing-functions#using-the-functionexternalmodules-option)

Not sure how or if it would affect the log time format though, anyone else know?  
[edit]  
To log with adjusted timestamp use  
console.log(moment.tz("Europe/London").format("YYYY-DD-MM HH:mm;ss"), "test")  
more info here [Logging : Node-RED](https://nodered.org/docs/user-guide/runtime/logging)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 November 2023 16:37 UTC](https://discourse.nodered.org/t/format-date-at-yyyydd-hh-mm-ss/83130/4 "2023-11-23T16:37:16Z")

</div>

And, of course, since you are using a modern Node.js, you don't need MomentJS at all for this, instead, the standard Date object formatting will do it just fine in a function node and will do local date/time formats too.

Just remember to think about whether you are using UTC (AKA Zulu or GMT) or using local timezones.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [23 November 2023 16:51 UTC](https://discourse.nodered.org/t/format-date-at-yyyydd-hh-mm-ss/83130/5 "2023-11-23T16:51:15Z")

</div>

> [@TotallyInformation](#):
>
> And, of course, since you are using a modern Node.js, you don't need MomentJS at all for this,

Yes of course, but moment makes the coding easier and more concise, in my opinion.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [23 November 2023 18:28 UTC](https://discourse.nodered.org/t/format-date-at-yyyydd-hh-mm-ss/83130/6 "2023-11-23T18:28:44Z")

</div>

The standard `Date` object in CJS I think is terrible on its own!  
its one of the most weak API's IMO.

I think my hatred towards it, is the fact its really not user friendly, and you end up with really ugly expressions to get what you want out of it.

I think the OP's attempt shows that

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 November 2023 22:17 UTC](https://discourse.nodered.org/t/format-date-at-yyyydd-hh-mm-ss/83130/7 "2023-11-23T22:17:22Z")

</div>

You need to use the INTL formatting options which let you do pretty much anything. But MomentJS is OK though it is no longer in production and is rather heavyweight just for date/time formatting.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [23 November 2023 22:22 UTC](https://discourse.nodered.org/t/format-date-at-yyyydd-hh-mm-ss/83130/8 "2023-11-23T22:22:05Z")

</div>

Yup.

I think day.js is “the new kid on the block” and replaces moment in a lot of frameworks I use.

I have not had much experience with it directly, but apparently is an almost drop in replacement.

_If you use Moment.js, you already know how to use Day.js._

> **[Day.js · 2kB JavaScript date utility library](https://day.js.org/)**
>
> 2kB JavaScript date utility library

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 November 2023 22:58 UTC](https://discourse.nodered.org/t/format-date-at-yyyydd-hh-mm-ss/83130/9 "2023-11-23T22:58:11Z")

</div>

Agree. Would like to update node-red-contrib-moment to use it but just don't have the time.

---

<div class="post-metadata">

**Author:** ![softy2k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/softy2k/32/23707_2.png) [@softy2k](https://discourse.nodered.org/u/softy2k)\
**Post date:** [24 November 2023 11:37 UTC](https://discourse.nodered.org/t/format-date-at-yyyydd-hh-mm-ss/83130/10 "2023-11-24T11:37:54Z")

</div>

Thanks for info about `day.js`

---

<div class="post-metadata">

**Author:** ![kitori](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@kitori](https://discourse.nodered.org/u/kitori)\
**Post date:** [24 November 2023 15:48 UTC](https://discourse.nodered.org/t/format-date-at-yyyydd-hh-mm-ss/83130/11 "2023-11-24T15:48:56Z")

</div>

i use day.js regulary. Example with Plugins:

```auto
[{"id":"9e0a19990afbd114","type":"function","z":"7636363de3130c9f","name":"day.js","func":"dayjs.extend(isBetween)\n\nconst now = dayjs()\nconst future = now.add(60,'second')\nconst between = now.add(30, 'second')\nlet calc_between = false\nlet new_payload = {}\n\nif (between.isBetween(now, future)) {\n calc_between = true\n }\n\n\nnew_payload['now'] = now\nnew_payload['now_formatted'] = now.format('YYYY-MM-DD HH:mm')\nnew_payload['future'] = future\nnew_payload['between'] = between\nnew_payload['calc_between'] = calc_between\n\n\n\nmsg.payload = new_payload\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"// Der Code hier wird ausgeführt,\n// wenn der Node gestoppt wird\nnode.status({fill:\"red\",shape:\"ring\",text:\"Waiting ... \"});","libs":[{"var":"dayjs","module":"dayjs"},{"var":"isBetween","module":"dayjs/plugin/isBetween"}],"x":630,"y":440,"wires":[["9618ddd0090b5fbe"]],"icon":"font-awesome/fa-calendar"},{"id":"d9b9a535ae3f139e","type":"inject","z":"7636363de3130c9f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":480,"y":440,"wires":[["9e0a19990afbd114"]]},{"id":"9618ddd0090b5fbe","type":"debug","z":"7636363de3130c9f","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":440,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [24 November 2023 17:36 UTC](https://discourse.nodered.org/t/format-date-at-yyyydd-hh-mm-ss/83130/12 "2023-11-24T17:36:16Z")

</div>

You don't need moment for this anymore as @TotallyInformation stated, it is part of javascript, moment is more convoluted imo.

```auto
const event = new Date('23 Nov 2023 17:45:00');
const jsonDate = event.toJSON();

node.warn(jsonDate);
// "2023-11-23T16:45:00.000Z" = UTC

node.warn(event.toLocaleString('en-GB', { timeZone: 'UTC' }));
// "23/11/2023, 16:45:00"

node.warn(event.toLocaleString('ko-KR', { timeZone: 'UTC' }));
// "2023. 11. 23. 오후 4:45:00"

node.warn(event.toLocaleString('us-US', { timeZone: 'America/Los_Angeles' }));
// "11/23/2023, 8:45:00 AM"

```

Recommendation: always use ISO8601 notation in logs.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [24 November 2023 17:38 UTC](https://discourse.nodered.org/t/format-date-at-yyyydd-hh-mm-ss/83130/13 "2023-11-24T17:38:22Z")

</div>

> [@bakman2](#):
>
> Recommendation: always use ISO8601 notation in logs.

And always work in UTC except for user input and user display.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [24 November 2023 17:52 UTC](https://discourse.nodered.org/t/format-date-at-yyyydd-hh-mm-ss/83130/14 "2023-11-24T17:52:48Z")

</div>

Try formating to "YYYY:MM:DD HH:mm;ss" as OP wanted, you will find it more convoluted, in my opinion. I read what @TotallyInformation said, I do not agree, as I clearly stated. Telling me again has not changed my opinion. Moments and dayjs have more functionality to (add, subtract, diff , between, etc).

```auto
moment().utc().format("YYYY:MM:DD HH:mm:ss")

```

Not that convoluted.

---

<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:** [23 January 2024 17:53 UTC](https://discourse.nodered.org/t/format-date-at-yyyydd-hh-mm-ss/83130/15 "2024-01-23T17:53:43Z")

</div>

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