简体   繁体   中英

Handle time zones from javascript to MySQL to javascript to MS SQL to javascript

I am using NodeJS/javascript to determine the current date which has the time zone of the system. Now i want to insert this date into MySQL with UTC+0. After that I want to select this date with NodeJS/javascript from MySQL again with UTC+0 and insert it now into MS SQL where I want it to be stored again as UTC+0. At the end I select with NodeJS/javascript again, but this time from the MS SQL and also I want the date this time to be in the timezone which is used right now in the system wherever I am in the future.

Here's an example: For 01.01.2018 15:00:00 UTC+1(German winter time) I want it to be stored in both databases with UTC+0. When summer comes and the time zone of my system changes to UTC+2(German summer time) or I move to another country with eg UTC-5 I want to get the date from MS SQL to be converted into the time zone my system uses automaticly.

So the questions are:

  • How can I insert my date from NodeJS with the systems time zone to MySQL with UTC+0?
  • How can I select this date, so that I can insert it with NodeJS into the MS SL and it's UTC+0 there too?
  • How can I select the date from MS SQL with NodeJS so that it has the systems time zone again?

Additional info: I have two programs in NodeJS communicating with each other. the first one (client) is inserting a date into his MySQL database and sends this to date also to the server application. the reason why he's inserting it into his own MySQL database is for synchronisation reasons when the server is not running, to get them another time. When the server receives the dates from the clients, those should be stored in the servers MS SQL database. admins of the server are able to export those dates into Excel whenever they want. but the dates have to be converted from the date in the MS SQL (UTC+0) into the servers system time zone, so that in the excel file the date is in the systems time zone instead of UTC+0

I think there are some optimizations which can be made across this solution, but I'll answer your questions first.

How can I insert my date from NodeJS with the systems time zone to MySQL with UTC+0?

You get the system time zone using new Date() - to convert that to UTC format which MySQL would understand you can do new Date().toISOString()

To store this value in MySQL you need a DateTime field. Just inserting that value should work, but if it doesn't just parse it for it to be a date.

How can I select this date, so that I can insert it with NodeJS into the MS SQL and it's UTC+0 there too?

Well you've inserted it in a standard format which is always UTC+0. You just select it and insert it the same.

How can I select the date from MS SQL with NodeJS so that it has the systems time zone again?

So - you have a DateTime field in MS SQL which you'll be querying through Node.JS - the field is in UTC+0 and you want to get that time accordingly for your server's timezone?

Well - if you do new Date().getTimezoneOffset() that should give you your timezone offset. So it should become something like this:

var date = MSSQLWrapper.someOperation('select d_field from ...');
var offset = new Date().getTimezoneOffset(); // Your offset
date.setHours(date.getHours() + offset); // Add your offset
console.log(date) // your aligned date.

Edit

If you want to change the timezone on selection from SQL you can use AT TIME ZONE

SELECT your_date from your_table   
AT TIME ZONE 'Eastern Standard Time' AS my_new_date

You can pass the timezone name in your query from your server side.

or alternatively you can check out TODATETIMEOFFSET

This question also has some good examples: Convert datetime value from one timezone to UTC timezone using sql query

Use moment js and its UTC feature . This way you will not need to handle the timezones.

Greetings

Dominik

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM