# Timestamp value change while insert and select from mariadb

**URL:** https://discourse.nodered.org/t/timestamp-value-change-while-insert-and-select-from-mariadb/3274
**Category:** General
**Created:** [19 September 2018 16:45 UTC](https://discourse.nodered.org/t/timestamp-value-change-while-insert-and-select-from-mariadb/3274 "2018-09-19T16:45:23Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![tramanah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tramanah/32/9122_2.png) [@tramanah](https://discourse.nodered.org/u/tramanah)
#### Post date: [19 September 2018 16:45 UTC](https://discourse.nodered.org/t/timestamp-value-change-while-insert-and-select-from-mariadb/3274/1 "2018-09-19T16:45:23Z")

</div>

**INSERT**

9/20/2018, 12:40:24 AM[node: 4dd4086a.fba828](http://127.0.0.1:1880/#)INSERT INTO mytab (DeviceId,Line,outPIN,count,Time) VALUES (?,?,?,?,?) : msg.payload : array[5]

[2, 1, 7, 1, "2018-09-20 00:40:24"]

**SELECT**  
9/20/2018, 12:40:36 AM[node: 9e0ad6c1.d131e8](http://127.0.0.1:1880/#)Select DeviceId, Line, outPIN, count,Time FROM mytab LIMIT 1 : msg.payload : string[82]

"[{"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-19T16:40:24.000Z"}]"

I did use the following function but still now changes  
var utc = json[0]["Time"].replace(/^(._)T(._).\d+Z$/, "$1 $2");

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [19 September 2018 18:32 UTC](https://discourse.nodered.org/t/timestamp-value-change-while-insert-and-select-from-mariadb/3274/2 "2018-09-19T18:32:58Z")

</div>

I've not used MariaDb, but it looks like it's expecting the input time to already be converted to the UTC timezone -- then when the same time is retrieved it is converted to the local timezone.

What are you using to generate that time string? One simple way is to install `node-red-contrib-moment`, and use it to convert that time from the local timezone to UTC before inserting in the db.

Best practice is to keep all timestamps in their internal format until it's time to display them to the end user. Otherwise, you will end up chasing your tail through different client timezones, and things like Daylight savings and leap seconds.

---

<div class="post-metadata">

### Author: ![tramanah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tramanah/32/9122_2.png) [@tramanah](https://discourse.nodered.org/u/tramanah)
#### Post date: [20 September 2018 03:15 UTC](https://discourse.nodered.org/t/timestamp-value-change-while-insert-and-select-from-mariadb/3274/3 "2018-09-20T03:15:07Z")

</div>

> [@shrickus](#):
>
> node-red-contrib-moment

Good day,

 ![timestamp](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/57d174bf07de37a377014062d4cbe1a25d609a46.png)

I do have "182018......" in my time stamp. How i can remove that, pls  
Regards  
Rama Tito

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [20 September 2018 03:51 UTC](https://discourse.nodered.org/t/timestamp-value-change-while-insert-and-select-from-mariadb/3274/4 "2018-09-20T03:51:59Z")

</div>

If there is nothing in the "Input From" field of the `moment` node, the default behavior is to use the current system timestamp -- which looks like what you are trying to do with all of that date string slicing/dicing in the `function` node. From the `moment` node's help information:

> Input
> 
> Must be either a JS datetime object or a [string that Moment.js](http://momentjs.com/docs/#/parsing/) can resolve (with the help of [moment.parseFormat](https://github.com/gr2m/moment-parseformat)). If the input is null, does not exist or is a blank string, the current Date/Time will be used.
> 
> This can be used to add a current timestamp to a flow of any kind easily.

So you can safely get rid of the function node, and clear the input field, and be guaranteed to have a valid timestamp output in whatever timezone and format you need. Check the help text for a link to the momentjs site, where it describes all the possible output formatting options...

---

<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: [20 September 2018 17:09 UTC](https://discourse.nodered.org/t/timestamp-value-change-while-insert-and-select-from-mariadb/3274/5 "2018-09-20T17:09:18Z")

</div>

> [@tramanah](#):
>
> [2, 1, 7, 1, "2018-09-20 00:40:24"]
> 
> **SELECT**  
> 9/20/2018, 12:40:36 AM[node: 9e0ad6c1.d131e8](http://127.0.0.1:1880/#)Select DeviceId, Line, outPIN, count,Time FROM mytab LIMIT 1 : msg.payload : string[82]
> 
> "[{"DeviceId":2,"Line":1,"outPIN":"7","count":1,"Time":"2018-09-19T16:40:24.000Z"}]"

Is the first time intended to be a local time? If so then I suspect the two times are the same.

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [15 March 2019 18:49 UTC](https://discourse.nodered.org/t/timestamp-value-change-while-insert-and-select-from-mariadb/3274/6 "2019-03-15T18:49:09Z")

</div>

> [@tramanah](#):
>
> I do have "182018......" in my time stamp. How i can remove that, pls

AH, I missed the formatting error when I responded last time... so for posterity, here is the fix:

You have specified your moment date format string as `YYY-MM-DD HH:MM:SS` _starting with 3 Y's_ instead of 4. Note also that `MM` represents the Month number (not Minutes), so your timestamp would be malformed as well...

---

<div class="post-metadata">

### Author: ![Lobo1979](https://avatars.discourse-cdn.com/v4/letter/l/779978/32.png) [@Lobo1979](https://discourse.nodered.org/u/Lobo1979)
#### Post date: [2 December 2019 19:02 UTC](https://discourse.nodered.org/t/timestamp-value-change-while-insert-and-select-from-mariadb/3274/7 "2019-12-02T19:02:48Z")

</div>

Hi all, I had some issue today with this kind of topic and here is one of the solution I had.  
When I was selecting data from MYSQL, where was timestamp column data in database and in chart was shifted with some amount in my case +10h. I was checking server settings timezone and it was set to New York even if we are in Europe in Slovakia. I changed this but it was not helpful. Now() command in MYSQL also was sowing local time without any problem. Than I checked MYSQL node settings and there is one small setting I committed called Timezone where I did not have set any option there. By clicking on it, it selected node-red option. Than when I selected data from databse again all was exactly as suppouse to be.

 ![mysqlnode](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3a6ca85afd2e6d682162ef21d647f9994b0c4b1d.jpeg)

---

<div class="post-metadata">

### Author: ![tramanah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tramanah/32/9122_2.png) [@tramanah](https://discourse.nodered.org/u/tramanah)
#### Post date: [13 June 2020 11:32 UTC](https://discourse.nodered.org/t/timestamp-value-change-while-insert-and-select-from-mariadb/3274/8 "2020-06-13T11:32:33Z")

</div>

Cheers, thats really helps

---

<div class="post-metadata">

### Author: ![ratandb](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@ratandb](https://discourse.nodered.org/u/ratandb)
#### Post date: [11 July 2020 08:37 UTC](https://discourse.nodered.org/t/timestamp-value-change-while-insert-and-select-from-mariadb/3274/9 "2020-07-11T08:37:24Z")

</div>

this worked for me  
thanks

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [11 July 2020 08:47 UTC](https://discourse.nodered.org/t/timestamp-value-change-while-insert-and-select-from-mariadb/3274/10 "2020-07-11T08:47:19Z")

</div>


