为什么来自NodeJS的SQL Server查询返回表示存储date前一天的date对象?

我正在写一个使用SQL Server作为数据存储的NodeJS应用程序。 节点使用mssql本地驱动程序连接到SQL Server。

当我用空的时间字段从数据库中select一个date对象时,返回存储date之前的date。 例如,如果date字段是2005年5月10日,则返回10/4/2005。

这里有完整的细节来重现:

首先,创build一个带有date时间列的表格:

/****** Object: Table [dbo].[testDate] Script Date: 8/28/2014 8:11:35 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[testDate]( [testID] [int] IDENTITY(1,1) NOT NULL, [testDate] [datetime] NULL ) ON [PRIMARY] GO 

然后我可以使用这个NodeJS代码来演示这个问题。 它在testDate表中创build一个新logging,select新logging,然后使用console.log输出date列。

这是插入查询:

 insert into testDate (testDate) values ('10/5/2005') 

然后运行一个select查询:

 select *from testDate where testID = 1 

这将返回新的行。 像这样的东西:

 testID: 1 testDate: 2005-10-05 00:00:00.000 

date的时间戳值为零,预期为2005年5月10日,这也是有意义的。 当使用Node内的mssql驱动程序select这个项目时,它会变成这样:

 Tue Oct 04 2005 20:00:00 GMT-0400 (Eastern Daylight Time) 

基本上,给我10/4/2005而不是10/5/2005。 我已经尝试了多个date,并能够一贯地复制这个问题。

我究竟做错了什么?

为了完整性,

  1. 如果我将时间戳添加到存储的date值(12:00或12:01),表示正确date的date对象在NodeJS中正确返回。
  2. 如果我使用不同的技术select相同的logging – 我试过ColdFusion – 返回的date对象是正确的。

这是一个完整的半可运行的NodeJS代码片段,它将插入一条新logging,select它,并将date列输出到控制台:

 // import the db driver var sql = require("mssql"); // set up the config to access the database var config = { user: 'sa', password: 'whatever', server: 'myserver.com', database: 'testDB' }; // Query to insert into database var query= "insert into testDate (testDate) values ('10/5/2005')"; var query= query + " SELECT SCOPE_IDENTITY() as testID"; console.log(query); // create connection var connection = new sql.Connection(config, function(err) { // when connection is made trigger off the first query var request = new sql.Request(connection); request.query(query, function(err, recordset){ // result handler for query 1 console.log(err); // undefined if everything is set up right console.log(recordset[0].testID); // the ID of the record we just inserted // select the item we just created from the database var query2= "select * from testDate where testID = " + recordset[0].testID; console.log(query2); // execute second query var connection = new sql.Connection(config, function(err) { var request = new sql.Request(connection); request.query(query2, function(err, recordset){ console.log(err); // undefined if everything is set up right console.log(recordset[0].testDate); // The Date; which is returning "Tue Oct 04 2005 20:00:00 GMT-0400 (Eastern Daylight Time" instead of Wednesday 10/5/2005 }); }) }); }) 

目前,我假设我做错了,而不是这是一个在MSSQL驱动程序的错误; 但是如果没有人能指出我做错了什么,我会很乐意在mssql github账户上打开一个问题。