# Payload.date string change himself to another format when build string variable

**URL:** <https://discourse.nodered.org/t/payload-date-string-change-himself-to-another-format-when-build-string-variable/83149>\
**Category:** General\
**Created:** [24 November 2023 15:05 UTC](https://discourse.nodered.org/t/payload-date-string-change-himself-to-another-format-when-build-string-variable/83149 "2023-11-24T15:05:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phildu77](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@Phildu77](https://discourse.nodered.org/u/Phildu77)\
**Post date:** [24 November 2023 15:05 UTC](https://discourse.nodered.org/t/payload-date-string-change-himself-to-another-format-when-build-string-variable/83149/1 "2023-11-24T15:05:13Z")

</div>

at the output of a function my payload is:  
{"variable":"index","value":1,"date":"2023-11-20T11:56:53.282Z","id\_mesure":653}

I pass this to an other function  
the payload stay the same but when extract the date string format change:

requete\_sql = "INSERT INTO t\_data (mesure\_FK,date\_heure,b) VALUES (" + id\_mesure + ",'" + msg.payload.date + "'," + valeur + ") ON DUPLICATE KEY UPDATE b=" + valeur;

now the string requete\_sql is:  
"INSERT INTO t\_data (mesure\_FK,date\_heure,b) VALUES (653,'Mon Nov 20 2023 12:56:53 GMT+0100 (heure normale d’Europe centrale)',1) ON DUPLICATE KEY UPDATE b=1"

so the date format changed to Mon Nov 20 2023 12:56:53 GMT+0100 (heure normale d’Europe centrale)

(I tried to use also an variable to store temporaly the msg.payload.date bur is the same)  
an idea please ?

---

<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:** [24 November 2023 15:14 UTC](https://discourse.nodered.org/t/payload-date-string-change-himself-to-another-format-when-build-string-variable/83149/2 "2023-11-24T15:14:47Z")

</div>

Welcome to the forums @Phildu77

Its because the debug pane is rendering the `Date` object to an ISO string via `Date.toJSON()`  
where as when you print as a string or it's part of another string it will call `Date.toString()`

And both of these produce different formats.  
You need to format the date to the required output before including it as part of another string (such as an SQL statement)

If the ISO string is the format you need.

```auto
msg.payload.date.toJSON()

```

```auto
requete_sql = "INSERT INTO t_data (mesure_FK,date_heure,b) VALUES (" + id_mesure + ",'" + msg.payload.date.toJSON() + "'," + valeur + ") ON DUPLICATE KEY UPDATE b=" + valeur;

```

---

<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:** [24 November 2023 15:27 UTC](https://discourse.nodered.org/t/payload-date-string-change-himself-to-another-format-when-build-string-variable/83149/3 "2023-11-24T15:27:26Z")

</div>

> [@marcus-j-davies](#):
>
> `msg.payload.date.toJSON()`

Personally I would prefer to use  
`msg.payload.date.toISOString()`

They have the same result but toISOString() is a bit clearer in what it will do. It is purely a matter of personal taste though.

---

<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:** [24 November 2023 15:29 UTC](https://discourse.nodered.org/t/payload-date-string-change-himself-to-another-format-when-build-string-variable/83149/4 "2023-11-24T15:29:12Z")

</div>

> [@Colin](#):
>
> Personally I would prefer to use  
> `msg.payload.date.toISOString()`

Is the better option actually, given it's not really going out as a JSON object.

---

<div class="post-metadata">

**Author:** ![Phildu77](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@Phildu77](https://discourse.nodered.org/u/Phildu77)\
**Post date:** [24 November 2023 15:56 UTC](https://discourse.nodered.org/t/payload-date-string-change-himself-to-another-format-when-build-string-variable/83149/5 "2023-11-24T15:56:06Z")

</div>

Perfect 🙂  
thanks a lot for your reactivity.  
both solutions work

---

<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:** [23 January 2024 15:56 UTC](https://discourse.nodered.org/t/payload-date-string-change-himself-to-another-format-when-build-string-variable/83149/6 "2024-01-23T15:56:44Z")

</div>

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