Transforming JavaScript UTC string and timezone offset to SQL Server UTC format

I’m working on a project where I need to store UTC time in a SQL Server datetime column. The tricky part is that the data comes from JavaScript in two parts:

  1. A UTC string made with (new Date()).toUTCString(). It looks like this: Thu, 03 Mar 2016 06:19:11 GMT

  2. A timezone offset created using new Date().getTimezoneOffset(). For example: -330

I’m pretty new to dealing with timezones and UTC, so I’m not sure how to combine these two pieces of info and save them correctly in SQL Server. Any tips on how to handle this would be super helpful!

Here’s a dummy code snippet to show what I’m working with:

function getTimeData() {
    const utcString = (new Date()).toUTCString();
    const tzOffset = new Date().getTimezoneOffset();
    return { utcString, tzOffset };
}

// Now I need to send this to SQL Server somehow

How can I take this JavaScript output and turn it into something SQL Server can understand as UTC time?

Hmm, interesting challenge! have you considered using momentjs library? it’s great for handling dates and timezones. you could parse the UTC string, apply the offset, and then format it for SQL Server. What backend language are you using? maybe we could brainstorm some specific solutions? :thinking:

hey SwiftCoder15, i’ve dealt with similar stuff before. For SQL Server, you don’t need to worry about the timezone offset. Just parse the UTC string into a DateTime object in your backend (C#, Java, etc.) and insert that directly. SQL Server will store it as UTC by default. no need to complicate things with the offset!