# LocalTime to MSSQL Server

**URL:** <https://discourse.nodered.org/t/localtime-to-mssql-server/43007>\
**Category:** General\
**Created:** [22 March 2021 13:33 UTC](https://discourse.nodered.org/t/localtime-to-mssql-server/43007 "2021-03-22T13:33:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pranavthakkar](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@pranavthakkar](https://discourse.nodered.org/u/pranavthakkar)\
**Post date:** [22 March 2021 13:33 UTC](https://discourse.nodered.org/t/localtime-to-mssql-server/43007/1 "2021-03-22T13:33:24Z")

</div>

Hi, I wanted to get the local time like this `2021-03-22 17:45:27.380`. and store it on the server.

I have tried the function node method but not able to get the upper one format.

Any suggestion on how can I get like that one.

My current function node code is

> msg.payload = "INSERT INTO [table].[dbo].[f] VALUES ('"+Date('now')+"','"+11+"')";  
> return msg;

result

> INSERT INTO [internet].[dbo].[freq] VALUES ('Mon Mar 22 2021 19:02:25 GMT+0530 (India Standard Time)','11')

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [22 March 2021 14:36 UTC](https://discourse.nodered.org/t/localtime-to-mssql-server/43007/2 "2021-03-22T14:36:19Z")

</div>

Do you really want _local_ time? This is generally not a good idea as you will end up with all sorts of confusing calculations to deal with DST, timezones, etc. Normally, the best approach is to store timestamps as UTC rather than local. Then convert to local time when actually displaying.

To get the right format:

```auto
const date = new Date().toISOString().slice(0, 19).replace('T', ' ').replace('Z','');

```

Or

```auto
const date = (new Date()).toLocaleString("en-US")

```

[Javascript date to sql date object - Stack Overflow](https://stackoverflow.com/questions/20083807/javascript-date-to-sql-date-object)

[javascript - Convert JS date time to MySQL datetime - Stack Overflow](https://stackoverflow.com/questions/5129624/convert-js-date-time-to-mysql-datetime)

---

<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:** [22 March 2021 14:37 UTC](https://discourse.nodered.org/t/localtime-to-mssql-server/43007/3 "2021-03-22T14:37:01Z")

</div>

Which MSSQL node are you using? Mssql-plus will permit you to do this with parameters (safe from SQL injection).

Note. You really should store UTC in your database and only when you retrieve it for display should you apply any timezone offset.

Lastly, if you must, mssql-plus has an option in the connection config to not use UTC (but I don't recommend it)

---

<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:** [21 May 2021 14:37 UTC](https://discourse.nodered.org/t/localtime-to-mssql-server/43007/4 "2021-05-21T14:37:12Z")

</div>

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