简体   繁体   中英

MySQL: date format changed when retrieving it by node.js

I have made a table with personal information with DOB as "Date" type. When I select the entire table in the terminal, it shows the correct format(YYYY-MM-DD). However, when I created an connection through node.js and output to the console, it was printed as a JSON object where the DOB now has hh-mm-ss behind the actual date?

For example one of my column has DOB as "1996-03-06". However, when it is passed to the node.js server and printe to the console log, it became "1996-03-0605:00:00.000Z"

What is more strange is that when I send this information to Ajax to print to the web, the output is actually something like this: Wed Mar 06 1996 00:00:00 GMT-0500 (EST), which is different from both the actual data and the data that was shown in the console

只需从Date对象打印或发送任何您需要的 ajax

console.log(DOB.toISOString().slice(0,10))

I ran into this issue my self. There is a connection param called dateStrings that defaults to false; This switched it back to traditional behavior.

const conn = await mysql.createConnection({
    host: process.env.DBHOST,
    user: process.env.DBUSER,
    password: process.env.DBPASS,
    port: process.env.DBPORT,
    database: process.env.DATABASE,
    dateStrings: true // <--- fix
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM