tediousjs / node-mssql

Microsoft SQL Server client for Node.js
https://tediousjs.github.io/node-mssql
MIT License
2.23k stars 465 forks source link

Raw date or datetime value #1524

Closed pols63 closed 1 year ago

pols63 commented 1 year ago

Hi. ¿Is it possible to get the raw date or datetime value before to be converted to javascript Date object?

I try to use "valueHandler"

sql.valueHandler.set(sql.TYPES.Date, (value) => value)

But "value" is already a Date object. This is an inconvenient for me due I have my database server at another timezone with respect to my nodejs server.

Expected behaviour:

In the next code, "value" must be a string with the format "yyyy-mm-dd"

sql.valueHandler.set(sql.TYPES.Date, (value) => value)

And, in next, "value" must be a string with the format "yyyy-mm-dd hh:ii:ss"

sql.valueHandler.set(sql.TYPES.DateTime, (value) => value) //this code not work, this function never run

That way I could have the opportunity to operate in a better way with the date value.

Actual behaviour:

In this code, value is already a Date object, but because of the time difference between servers, the value is different too. For example, in database, I have '2023-08-12' but "value" is "2023-08-11T19:00:00Z"

sql.valueHandler.set(sql.TYPES.Date, (value) => value)

Configuration:

I'm using

options: { useUTC: false }

Software versions

pols63 commented 1 year ago

Hi. I'm sorry. "useUTC" parameter was "undefined". I resolve my problem setting "useUTC" after the connection object was created.

connection.config.options.useUTC = false

Even so, this code never work:

sql.valueHandler.set(sql.TYPES.DateTime, (value) => {
console.log(value)
return value
})

Thanks!

dhensby commented 1 year ago

Even so, this code never work:

Datetimes in SQL are a bit funny, it's probably being executed as a SmallDateTime.

It is not possible to get the raw value as a string for datetimes because the underlying driver returns them as JS Date objects. If you want it as text, you'll have to cast it as text.