Skip to content

node-mssql v6 incorrectly stores Dates #3202

Description

@pburrows

Issue type:
[x ] question
[x] bug report

Database system/driver:
[x] mssql

TypeORM version:
[x] latest

Steps to reproduce or a small repository showing the problem:

I am trying to write a date, devoid of timezone information (a UTC date), to my SQL Server database. I created a test project to try it out.

(NOTE: I am in the eastern timezone)

Here are some of the ways I am trying to write the date '12/1/2018' to the database:

The most basic:

const test2: TestTable = new TestTable();
test2.testInfo = 'new Date(2018, 11, 1)';
test2.testDate = new Date(2018, 11, 1);
await TestDb.GETMANAGER().save(test2);

writes the following record to the database:

image

This date is off by 24 hours!!!!

The SQL generated by TypeOrm is this:

INSERT INTO "TestTable"("testInfo", "testDate") OUTPUT INSERTED."tableId" VALUES (@0, @1) -- PARAMETERS: [{"value":"new Date(2018, 11, 1)","type":"nvarchar","params":[]},{"value":"2018-12-01T05:00:00.000Z","type":"datetime2","params":[]}]

Which, if you put the parameters into the query you get this sql statement:

INSERT INTO "TestTable"("testInfo", "testDate") OUTPUT INSERTED."tableId" VALUES ('new Date(2018, 11, 1)', '2018-12-01T05:00:00.000Z') 

If you run THAT SQL Statement in SQL Server Management Studio (by copy and pasting), it writes this record to the database:

image

Which isn't exactly what I am looking for, but is wrong in a way I understand (it added my local timezone to the date string it generated) and that I can account for.

Why is the data written to the database different when the SQL Statement is run manually vs run by TypeORM?

How do I correctly write a UTC date to the database?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions