LocalTime to MSSQL Server

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')

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:

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

Or

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

Javascript date to sql date object - Stack Overflow

javascript - Convert JS date time to MySQL datetime - Stack Overflow

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)

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