Another question about time and formats

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:

// 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.

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_);

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

In what context?

From a CLI or from a node?

CLI initially.

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)

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

1 Like

Tue 15 Oct 19:26:52 AEDT 2024

I am not sure where this is going.

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.

(Sorry the code - from Chat GPT)

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;

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.

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

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

This is almost what you were after

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
2 Likes

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

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