0

I am trying to get SQL Server date as a date object using the npm mssql package. Unfortunately it returns type = object (Orderdate is a datetime object in SQL Server).

How can I easily return the date column as an actual node.js Date object?

const sql = require('mssql')
const ordersSelect = `
SELECT TOP 20
OrderDate
FROM Orders
`
let pool = await sql.connect('mssql://sa:wonton$98@TS1\\SQL2014/Artoo')
const request = pool.request()
const orders = await request.query(ordersSelect)
var orders = orders.recordset

1 Answer 1

2

As far as I know, the Node module mssql cannot be configured to return SQL datetimes to Node date objects. Everything just comes back as strings inside the recordset object

However you could write some code to transform the data set after it has been loaded by the module. JavaScript can convert strings from the format yyyy-mm-dd hh:mm:ss.sss (returned by SQL Server) to its own Date object, so something like

for (var row in orders) {
    orders[row].OrderDate = new Date(orders[row].OrderDate)
    // or
    orders[row]["OrderDate"] = new Date(orders[row]["OrderDate"])
}

to the end of your code sample provided would work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.