How to format time

Sorry folks, but I am a bit stuck.

This is the code I have that works.

let now = Date();
msg.time = now;
return msg;

But I am only wanting a basic time output of HH:mm:ss

How would I get that - easily. I know of the moment node.
But I want to keep this VERY simple. Which I know is out of character for me. :wink:

Use now.toLocalTimeString();

You greatly over estimate my intelligence.

I see words but that's it.

I can't translate that to anything I can use.

I thought it was obvious:

let now = Date();
msg.time = now.toLocaleTimeString();
return msg;


  • Fixed the typo @ .toLocaleTimeString()
  • Readers are encouraged to read on - as there's another issue in this sniplet.

Maybe. But Node-red doesn't like it.

gets me

TypeError: now.toLocalTimeString is not a function

I'm using this:

function addZero(i) {
    if (i < 10) { i = "0" + i }
    return i;
const ts = new Date()
let hhmmss = addZero(ts.getHours()) + ":" + addZero(ts.getMinutes()) + ":" + addZero(ts.getSeconds())
msg.payload = hhmmss
return msg
1 Like

Changed msg.payload to msg.time but all works.

These tricks with functions are still above my head.
(But I am a short person)

const now = new Date()
msg.time = now.toLocaleTimeString()
return msg

Ok, in the error I posted above I left in the msg.time = now line.

But I had that middle line in there - didn't I?

It threw an error which I didn't / don't understand.

Your error was 2 fold.

  1. You did not specify new so now was not a proper Date object. So you could not call toLocaleTimeString on the uninstantiated object now
  2. You also had an unnecessary line msg.time = now

Alas I've deleted the original code.

Looking at what I see in the screen shot - function node:

let now = Date();

msg.time = now.tolocalTimeString();

msg.time = now;
return msg;

Sorry, but where is new in this?

You did not specify new so.....

I am fretting just now as I have seen ANOTHER problem with some code I posted in an effort to help some one.

It will work, but there is a problem.

This ^&^& OCD really REALLY sucks.

I am now trying to get the problem I see fixed.
All this was a simple thing to make it a BASIC time printout showing time stamps.
Not really crucial to things. Just to make it nicer to read.

The difference

let now = new Date()

When called as a constructor, returns a new Date object

let now = Date()
When called as a function, returns a string representation of the current date and time. All arguments are ignored. The result is the same as executing new Date().toString()

Hope you get your problem fixed

1 Like


I read that new (somehow) as part of the Date() command.


Also you had local instead of locale.

1 Like

There is also the conflict of vocabulary and auto correction with computer terms.
local and locale.

local and locale are perfectly good English words, meaning different things. Local is an adjective, locale is a noun.


But my spell corrector is pathetic.
It puts the wrong words in sometimes so really I should disable it.
(If only I could - it is very bad muscle memory on how to spell things)

So that bites me a lot when trying to write code.

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