# Function to set a timestamp in msg.timestamp - error

**URL:** <https://discourse.nodered.org/t/function-to-set-a-timestamp-in-msg-timestamp-error/83466>\
**Category:** General\
**Created:** [6 December 2023 13:36 UTC](https://discourse.nodered.org/t/function-to-set-a-timestamp-in-msg-timestamp-error/83466 "2023-12-06T13:36:51Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![spitzlbergerj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spitzlbergerj/32/17228_2.png) [@spitzlbergerj](https://discourse.nodered.org/u/spitzlbergerj)\
**Post date:** [6 December 2023 13:36 UTC](https://discourse.nodered.org/t/function-to-set-a-timestamp-in-msg-timestamp-error/83466/1 "2023-12-06T13:36:52Z")

</div>

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 🙈

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.

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [6 December 2023 13:48 UTC](https://discourse.nodered.org/t/function-to-set-a-timestamp-in-msg-timestamp-error/83466/2 "2023-12-06T13:48:10Z")

</div>

> [@spitzlbergerj](#):
>
> Marked is the line var now = new Date();

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)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 December 2023 14:17 UTC](https://discourse.nodered.org/t/function-to-set-a-timestamp-in-msg-timestamp-error/83466/3 "2023-12-06T14:17:15Z")

</div>

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

Can you share the exact error?

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [6 December 2023 14:19 UTC](https://discourse.nodered.org/t/function-to-set-a-timestamp-in-msg-timestamp-error/83466/4 "2023-12-06T14:19:54Z")

</div>

> [@E1cid](#):
>
> I am seeing no error, is this snippet of code part of a larger section of code?

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [6 December 2023 14:25 UTC](https://discourse.nodered.org/t/function-to-set-a-timestamp-in-msg-timestamp-error/83466/5 "2023-12-06T14:25:13Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/538e37956679bd88829b11c2645e1eb8fe4b93ec.png)

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [6 December 2023 14:31 UTC](https://discourse.nodered.org/t/function-to-set-a-timestamp-in-msg-timestamp-error/83466/6 "2023-12-06T14:31:14Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [6 December 2023 15:45 UTC](https://discourse.nodered.org/t/function-to-set-a-timestamp-in-msg-timestamp-error/83466/7 "2023-12-06T15:45:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [6 December 2023 16:11 UTC](https://discourse.nodered.org/t/function-to-set-a-timestamp-in-msg-timestamp-error/83466/8 "2023-12-06T16:11:58Z")

</div>

> [@jbudd](#):
>
> Wait! You mean there is a way to get rid of those ~~damned~~ useful popups?

> <https://github.com/node-red/node-red/blob/f3dd5770d94b9592113255833907d198dbe85dca/packages/node_modules/node-red/settings.js#L410>

I still use `ace` as I'm that good, don't need the help 😉

---

<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:** [6 December 2023 19:46 UTC](https://discourse.nodered.org/t/function-to-set-a-timestamp-in-msg-timestamp-error/83466/9 "2023-12-06T19:46:50Z")

</div>

> [@spitzlbergerj](#):
>
> `msg.timestamp = now.toLocaleDateString('de-DE', options);`

Try

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

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [6 December 2023 20:07 UTC](https://discourse.nodered.org/t/function-to-set-a-timestamp-in-msg-timestamp-error/83466/10 "2023-12-06T20:07:13Z")

</div>

> [@marcus-j-davies](#):
>
> I tested the above code as is - and couldn't see any error either,

> [@marcus-j-davies](#):
>
> I still use `ace` as I'm that good, don't need the help

Clearly not 😉

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

![chrome_Ikburk0n4m](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d347ec60fa468b1fde387db085c5da8a4553258d.gif)

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

---

<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:** [6 December 2023 20:13 UTC](https://discourse.nodered.org/t/function-to-set-a-timestamp-in-msg-timestamp-error/83466/11 "2023-12-06T20:13:04Z")

</div>

> [@spitzlbergerj](#):
>
> However, I am constantly warned that there is probably still an error in the function.

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:

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

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [6 December 2023 20:20 UTC](https://discourse.nodered.org/t/function-to-set-a-timestamp-in-msg-timestamp-error/83466/12 "2023-12-06T20:20:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![spitzlbergerj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spitzlbergerj/32/17228_2.png) [@spitzlbergerj](https://discourse.nodered.org/u/spitzlbergerj)\
**Post date:** [7 December 2023 07:53 UTC](https://discourse.nodered.org/t/function-to-set-a-timestamp-in-msg-timestamp-error/83466/13 "2023-12-07T07:53:12Z")

</div>

> [@Buckskin](#):
>
> ```auto
> 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
> 
> ```

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

---

<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:** [5 February 2024 07:53 UTC](https://discourse.nodered.org/t/function-to-set-a-timestamp-in-msg-timestamp-error/83466/14 "2024-02-05T07:53:52Z")

</div>

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