Function to set a timestamp in msg.timestamp - error

Hello,
unfortunately I can't get any further with my research, so I'm asking here. It's probably a small thing, but sorry, I just don't see the error anymore :see_no_evil:

I have written the following function to fill msg.timestamp with the timestamp. This is then written later in the flow as a timestamp in a log file. The function also works, the file contains the correct timestamps. However, I am constantly warned that there is probably still an error in the function.

var options = {
        hour: 'numeric',
        minute: 'numeric',
        second: 'numeric'
    };
var now = new Date();
msg.timestamp = now.toLocaleDateString('de-DE', options);
return msg;

Marked is the line var now = new Date();

Thanks for any help

Regards
Sepp

On my computer, the line giving the warning is msg.timestamp = now.toLocaleDateString('de-DE', options);

Overload 1 of 3, '(locales?: LocalesArgument, options?: DateTimeFormatOptions): string', gave the following error.
Argument of type '{ hour: string; minute: string; second: string; }' is not assignable to parameter of type 'DateTimeFormatOptions'.
Types of property 'hour' are incompatible.
Type 'string' is not assignable to type '"numeric" | "2-digit"'.
Overload 2 of 3, '(locales?: string | string, options?: DateTimeFormatOptions): string', gave the following error.
Argument of type '{ hour: string; minute: string; second: string; }' is not assignable to parameter of type 'DateTimeFormatOptions'.(2769)

I am seeing no error, is this snippet of code part of a larger section of code?

Can you share the exact error?

I tested the above code as is - and couldn't see any error either, and received the expected output

Would that be platform related?
This is what I see on RPiOS Bullseye 64 bit, node.js v18.18.2
and Node-red 3.1.0

Ah!

I don't use the version of the code editor that has intellisense, so maybe will show the error If I use it.
But it still produces what I need - so maybe a small mis-reported error?

Wait! You mean there is a way to get rid of those damned useful popups?

I still use ace as I'm that good, don't need the help :wink:

3 Likes

Try

const options = {
    year: "numeric",
    month: "numeric",
    day: "numeric",
    hour: 'numeric',
    minute: 'numeric',
    second: 'numeric'
}

let now = new Date()

msg.timestamp = new Intl.DateTimeFormat('de-DE', options).format(now)

return msg

Clearly not :wink:

There is far more than annoying popups like "good practice" and "possible options" and code formatting and more..

chrome_Ikburk0n4m

I live with the "annoying popups" and ignore them, until I need them.

1 Like

I'll probably get shot down for this, but according to chatGPT...


The provided JavaScript code appears to be mostly correct, but there's a small issue in the toLocaleDateString method. The toLocaleDateString method is primarily used for formatting dates, and it doesn't support formatting hours, minutes, and seconds. To format the time as well, you should use toLocaleString instead.

Here's the corrected code:

var options = {
    hour: 'numeric',
    minute: 'numeric',
    second: 'numeric'
};
var now = new Date();
msg.timestamp = now.toLocaleString('de-DE', options);
return msg;

...which although works OK, the variable options is underlined red in this line;
msg.timestamp = now.toLocaleString('de-DE', options);

But I have "the worlds best software developer" mug, it's not like everyone has one right? :wink:

1 Like

Hi @Buckskin , thanks a lot. Now I get no more errors!!

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