1

EDIT: Some users noted that this is a duplicate of ADO.NET question at How can I get DateTime data from SQL Server ignoring time zone issues?

Please DO READ my tags, and my codes. This is nodejs question, that has nothing to do with ADO library whatsoever. It doesn't even run on Windows. The option noted at the link provided doesn't even exists at mssql-npm

I am creating a database for company attendance, which spans for 3 timezones.

The problem with SQL Server is that I read the working hour differently from each timezone. It registered like 1969-12-31T23:00:00.000Z, and registers as 06:00 for one timeline, and 08:00 for another. I need it to be exactly at 08:00 regardless of timezone. It means, 08:00 at western part of my country, and also 08:00 at eastern part of my country.

When I try to disable UseUTC, nodejs translated my timezone twice. For example, this is when I sent masuk field as 08:00


Sent to server:

Sent to server


Query sent to SQL Server:

UPDATE shift SET name = 'Shift 1', masuk = '2019-10-28T01:00:00.000Z', keluar = '2019-10-28T10:00:00.000Z', tolerance = 15 WHERE id = 1


Received the result back

result


And shown to the user as:

Show to user

My country sits at GMT+7 to GMT+9. I don't mind if I have to flatten the timezone. But how to do that? Is there a way to read the time completely ignoring the time zone? It makes my work unnecesarily complicated.

I am using:

  • SQL Server 2012
  • Material UI
  • date-fns
  • Nodejs 12
  • mssql module

and this is my code to get the time from user input

<MuiPickersUtilsProvider utils={DateFnsUtils}>
    <KeyboardTimePicker
        key={component.field.toString()}
        variant="inline"
        value={value}
        label={component.title}
        onChange={this.eventHandler(component.field).bind(this)}
        format="HH:mm"
    />
</MuiPickersUtilsProvider>

Thank you for help.

THe database uses time(7) to store the time

Magician
  • 1,944
  • 6
  • 24
  • 38
  • You would better convert local time to UTC before storing the date in the database. – GMB Oct 28 '19 at 09:17
  • @GMB I can't.. Because that would be 01:00, 00:00, and 23:00. Working hour calculation would be extremely complicated since the server resides at GMT+7. – Magician Oct 28 '19 at 09:19
  • @GMB I need to either flatten all of them to 01:00, or remove timezone completely and store it as 08:00. But I don't know how. Been looking for a way to do that. – Magician Oct 28 '19 at 09:22
  • What datatype are the datetime fields in the database? Note that in your reponse the dates aren't even close. They're different decades. – Nick.Mc Oct 28 '19 at 09:55
  • @Nick.McDermaid it is `time(7)` – Magician Oct 28 '19 at 09:58
  • It's not SQL Server's fault. Have you read [the `mssql` documentation](https://www.npmjs.com/package/mssql)? `options.useUTC` defaults to `true`. – AlwaysLearning Oct 28 '19 at 10:00
  • @AlwaysLearning As I have noted, I have set it to `false`. True messed everything. – Magician Oct 28 '19 at 10:02
  • OK the date component is irrelevant here, only the time matters. The `time` data type is not timezone aware. So you need save the timezone (or location) in the record. Otherwise you don't know what it's equivalent local time is. So how are you doing that? BTW what happens when a shift passes midnight? Do you just assume if the time is earler, it went over midnight? – Nick.Mc Oct 28 '19 at 10:11
  • if you are using tedious please also tag that. – Nick.Mc Oct 28 '19 at 10:12
  • @Nick.McDermaid Ok.. Tedious tagged.. Yes.. when end time is lower than start, it will assume it passes midnight. So I combine current date and shift time, then compare to the log, and find the closest time. – Magician Oct 29 '19 at 02:06
  • @Nick.McDermaid I don't mind for attendance log to have a timezone, but I need the scheduled shift to be free of timezone, and retrieved as it is, without any conversion to current timezone. Actually, I need the timezone for attendance log, but I want shift time to be equal across any timezone. – Magician Oct 29 '19 at 02:11
  • I think I may have misunderstood. Can you clarify: the time in the database is always correct, but when you use your application across different timezones it incorrectly has the timezone added? Are you saying that times are actually recorded in the database as UTC? That basically just means your app needs to have a fixed timezone, not an application server or client side dependent one. – Nick.Mc Oct 29 '19 at 02:15
  • @Nick.McDermaid Well.. direct SQL Query from Azure studio always gives correct time, but then nodejs/javascript always translate the timezone. I am not sure how to remove it for this particular time(7). I have included the screenshot about how data exchanged between server and client, and they are not the same with the input. It sends different data to the server, server sent same data to the mssql. Same problem from mssql. Azure data studio shows 1:00 as recorded from 08:00 entry before, then query with mssql-npm, gave me 18:00, as reflected at data received from server above. – Magician Oct 29 '19 at 03:56

0 Answers0