3

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

3
  • It looks like maybe Node converted the dates to timestamps set to midnight. Nothing wrong with this, just display as much information as you want. Commented Aug 24, 2017 at 4:19
  • However, when I send this piece of information thru Ajax and print to the web, the output becomes: 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. how do I make it so that I just show YYYY-MM-DD? Commented Aug 24, 2017 at 4:22
  • can you upload that part of code you are sending using node.js? Commented Aug 24, 2017 at 5:03

2 Answers 2

6

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
  });
Sign up to request clarification or add additional context in comments.

Comments

0

Just print or send in ajax whatever you need from the Date object

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

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.