# Another question about time and formats

**URL:** <https://discourse.nodered.org/t/another-question-about-time-and-formats/92392>\
**Category:** General\
**Created:** [15 October 2024 02:18 UTC](https://discourse.nodered.org/t/another-question-about-time-and-formats/92392 "2024-10-15T02:18:30Z")\
**Posts on this page:** 13\
**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 02:18 UTC](https://discourse.nodered.org/t/another-question-about-time-and-formats/92392/1 "2024-10-15T02:18:30Z")

</div>

Ok, I kinda got the other question working/resolved.

But in doing so I found _ANOTHER_ gremlin lurking under the covers.

`new Date().toLocaleString();`

For reasons unknown to me, it gives me time with those pesky AM/PM things.

I want 24 hour time. LOCAL.

Seems that bit of code has snuck into a few nodes I am using.

How do I get it to give me the time in 24 hour format?

Please.

Given this code gives me 24 hour time:

```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;

```

And this code just has an error with the `options` part - as mentioned in the other thread.

```auto
const now = new Date();

// Set options for 24-hour format
const options = {
    hour: '2-digit',
    minute: '2-digit',
    hour12: false // Set to false for 24-hour format
};

// Get the time in 24-hour format
const time_ = now.toLocaleString('en-AU', options);

console.log(time_);

```

---

<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:** [15 October 2024 08:00 UTC](https://discourse.nodered.org/t/another-question-about-time-and-formats/92392/2 "2024-10-15T08:00:56Z")

</div>

> [@Trying\_to\_learn](#):
>
> `new Date().toLocaleString();`
> 
> For reasons unknown to me, it gives me time with those pesky AM/PM things.

Assuming this is a Pi, what does the `date` command show?

---

<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 08:11 UTC](https://discourse.nodered.org/t/another-question-about-time-and-formats/92392/3 "2024-10-15T08:11:00Z")

</div>

> [@Colin](#):
>
> what does the `date` command show?

In what context?

From a CLI or from a node?

---

<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:** [15 October 2024 08:19 UTC](https://discourse.nodered.org/t/another-question-about-time-and-formats/92392/4 "2024-10-15T08:19:06Z")

</div>

CLI initially.

---

<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 08:20 UTC](https://discourse.nodered.org/t/another-question-about-time-and-formats/92392/5 "2024-10-15T08:20:49Z")

</div>

```auto
date
Tue 15 Oct 19:19:23 AEDT 2024

```

Which is good.

Though the _Order_ of its formatting sometimes needs changing - in NR.  
(Or maybe better expressed as _STRUCTURE_... Dunno)

---

<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:** [15 October 2024 08:23 UTC](https://discourse.nodered.org/t/another-question-about-time-and-formats/92392/6 "2024-10-15T08:23:36Z")

</div>

Is it the same if you run `date` in an exec node? I had expected toLocaleString to use the system format.

---

<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 08:31 UTC](https://discourse.nodered.org/t/another-question-about-time-and-formats/92392/7 "2024-10-15T08:31:44Z")

</div>

`Tue 15 Oct 19:26:52 AEDT 2024`

I am not sure where this is going.

> [@Trying\_to\_learn](#):
>
> And this code just has an error with the `options` part - as mentioned in the other thread.

`const time_ = now.toLocaleString('en-AU', options);`

Could you indulge the idiot with what is going on t/here?

ChatGPT spits it to me but when I type it NR gives me an error (red line under `options`) and the text is needing a rogue scholar to understand it.

 ![Screenshot from 2024-10-15 19-31-14](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b2296c0fe76809f2d7724ce464331a76d3b6af29.png)

(Sorry the code - from Chat GPT)

```auto
const now = new Date();

// Set options for 24-hour format
const options = {hour: '2-digit', minute: '2-digit', hour12: false};

// Get the time in 24-hour format
const time_ = now.toLocaleString('en-AU', options);

msg.payload = time_

return msg;

```

---

<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 08:38 UTC](https://discourse.nodered.org/t/another-question-about-time-and-formats/92392/8 "2024-10-15T08:38:21Z")

</div>

I think `Date/Time Formatter` is a good node.

I send it a _timestamp_ (somewhere in the message) and I can adjust the _format_ in there.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [15 October 2024 09:04 UTC](https://discourse.nodered.org/t/another-question-about-time-and-formats/92392/9 "2024-10-15T09:04:53Z")

</div>

I thought that you used the simpletime node in your flows, to manipulate the date/time?

---

<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:10 UTC](https://discourse.nodered.org/t/another-question-about-time-and-formats/92392/10 "2024-10-15T09:10:54Z")

</div>

Honestly Paul, this is a problem/thing that recently popped up needing my attention and I was stuck with how to _format_ the time/date (date/time) to a specific format.

I'm going around reformatting the date/time on logs.

So rather than getting (example) `2024/10/15 18:06:33`  
I just need `15/10 18:06:33`

Then I have the DATE, MONTH and time.

Then the log file (although already in that order) you see the date, then month, then time. Then the event that happened.

The older code did do it, but as I only now realised has AM/PM time.  
(Cringe)

So I was trying to get it in 24 hour time.  
Asked ChatGPT and got a bit of code, but the code says the `options` is invalid.  
Yeah, it (chat GPT) isn't perfect.

But I don't understand why it offered it to me and what is wrong with it and where the `options` comes into how it works.

All my other excursions into JS on other sites didn't really mention the `options` or didn't explain it well enough.  
It just _Seems to be taken as it is globally understood_ how it works..

---

<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:20 UTC](https://discourse.nodered.org/t/another-question-about-time-and-formats/92392/11 "2024-10-15T19:20:12Z")

</div>

This is almost what you were after

```auto
const now = new Date()

// Set options for 24-hour format and day/date
const options = {
    day:'2-digit',
    month: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    hour12: false // Set to false for 24-hour format
}

// Get the time in 24-hour format with date as day/month (example: 15/10, 20:16 )
const dateTime = new Intl.DateTimeFormat('en-AU', options).format(now)

msg.time = dateTime

return msg

```

---

<div class="post-metadata">

**Author:** ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)\
**Post date:** [16 October 2024 03:51 UTC](https://discourse.nodered.org/t/another-question-about-time-and-formats/92392/12 "2024-10-16T03:51:45Z")

</div>

When working with js syntax, I prefer using a sandbox repl like [https://playcode.io/javascript](https://playcode.io/javascript)

---

<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:** [30 October 2024 03:52 UTC](https://discourse.nodered.org/t/another-question-about-time-and-formats/92392/13 "2024-10-30T03:52:28Z")

</div>

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