# Help to make a quick bit of code with Date/Time and message

**URL:** <https://discourse.nodered.org/t/help-to-make-a-quick-bit-of-code-with-date-time-and-message/92390>\
**Category:** General\
**Created:** [15 October 2024 01:21 UTC](https://discourse.nodered.org/t/help-to-make-a-quick-bit-of-code-with-date-time-and-message/92390 "2024-10-15T01:21:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [15 October 2024 01:21 UTC](https://discourse.nodered.org/t/help-to-make-a-quick-bit-of-code-with-date-time-and-message/92390/1 "2024-10-15T01:21:49Z")

</div>

(Don't know why but some of the suggestions offered at the start won't open)

I asked `chatGPT` and it doesn't seem to work.

```auto
// Get the current date and time
let now = new Date();

// Format the date to a human-readable string
let options = { 
    year: 'numeric', 
    month: 'long', 
    day: 'numeric', 
    hour: '2-digit', 
    minute: '2-digit', 
    second: '2-digit', 
    timeZoneName: 'short' 
};

// Convert to human-readable format
msg.payload = now.toLocaleString('en-AU', options); // Change 'en-US' to your preferred locale

return msg;

```

The `options` part throws an error at me.

I want a node that accepts a payload (used) and makes a human readable date/time mark and appends the payload.

Sorry, brain not working.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [15 October 2024 02:12 UTC](https://discourse.nodered.org/t/help-to-make-a-quick-bit-of-code-with-date-time-and-message/92390/2 "2024-10-15T02:12:10Z")

</div>

This code seems to work.

```auto
// Get the current date and time
let now = new Date();

let message = msg.payload

// Extract the components
let year = now.getFullYear();
let month = String(now.getMonth() + 1).padStart(2, '0'); // Months are 0-based
let date = String(now.getDate()).padStart(2, '0');
let hours = String(now.getHours()).padStart(2, '0');
let minutes = String(now.getMinutes()).padStart(2, '0');

// Format the payload
msg.payload = `${year}/${month}/${date} ${hours}:${minutes}` + " " + message;

return msg;

```

Given `msg.payload` has a message I want included in the outgoing message.

---

<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:** [15 October 2024 09:32 UTC](https://discourse.nodered.org/t/help-to-make-a-quick-bit-of-code-with-date-time-and-message/92390/3 "2024-10-15T09:32:23Z")

</div>

Stupid TS checker.

Add this on the line before the error:

```auto
// @ts-ignore

```

🙂

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [15 October 2024 09:34 UTC](https://discourse.nodered.org/t/help-to-make-a-quick-bit-of-code-with-date-time-and-message/92390/4 "2024-10-15T09:34:28Z")

</div>

Thanks but you lost me.

---

<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:** [15 October 2024 09:35 UTC](https://discourse.nodered.org/t/help-to-make-a-quick-bit-of-code-with-date-time-and-message/92390/5 "2024-10-15T09:35:52Z")

</div>

It is a case of the TypeScript engine not understanding how to use that JavaScript function properly. So you can ignore the error. Using `@ts-ignore` in the comment before the line will tell the Monaco editor to ignore the error.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [15 October 2024 09:39 UTC](https://discourse.nodered.org/t/help-to-make-a-quick-bit-of-code-with-date-time-and-message/92390/6 "2024-10-15T09:39:12Z")

</div>

WOW!

That is _left of field_ for me. (dunno if that is the right saying.)

(Good news / Bad news though)

I have since found `Date/Time Formatter` node.

That allows me to adjust the _structure_ of the output.  
That seems to do a a nicer job and is easier to use.

But I shall have to really try to remember that _trick_.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [15 October 2024 19:23 UTC](https://discourse.nodered.org/t/help-to-make-a-quick-bit-of-code-with-date-time-and-message/92390/7 "2024-10-15T19:23:25Z")

</div>

Try

```auto
msg.payload = new Intl.DateTimeFormat('en-AU', options).format(now)

```

instead of

```auto
msg.payload = now.toLocaleString('en-AU', options)

```

---

<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:** [29 October 2024 19:23 UTC](https://discourse.nodered.org/t/help-to-make-a-quick-bit-of-code-with-date-time-and-message/92390/8 "2024-10-29T19:23:34Z")

</div>

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