2

I created a page using handlebars where users can input a date, I works correctly and it shows the correct format on my mysql database, like this: 2017-04-01.

But when I get the date form the database using node js:

connection.query('SELECT date FROM tbl', function(err, rows, fields){
    console.log(rows);
});

I get this output:

Sat Apr 01 2017 00:00:00 GMT+0800 (China Standard Time)

Why does it include the time? but on the database is just the date. This is a problem for me because I need the date to output on a page and it takes too much space.

4
  • Just format your date Commented Apr 11, 2017 at 8:37
  • You can either use SQL or JS Date object to get the date into the correct format. It depends on which libraries you use and what the initial format of the SQL table is. Commented Apr 11, 2017 at 8:37
  • 1
    It's because (I'm assuming) it's parsing it to a JS Date object which also stores the time, the good news is you can format it to only show the date values Commented Apr 11, 2017 at 8:37
  • @George how? can you please show me an example of the code. Commented Apr 11, 2017 at 8:38

1 Answer 1

1

You are getting a JS Date object and by default, JS display you everything he knows about this date like time even if it is 0h0m0s (source : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Date)

If you only want to display only the day for example, you can do this :

var myFormattedDate = date.toISOString().substring(0, 10);

Look here for explanation : https://stackoverflow.com/a/28431880/4541118

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

5 Comments

Did you just assume the JS Engine's gender?
My JS engine is a cat, whom I have to coerce with snacks and attention.
Well either way we can assume it's a binary gender ba dum dum tish
How do I exactly use that code? for example I have an object that has a date value, lets name in myDate. And when I output myDate I get the wrong format, how do I use var myFormattedDate = date.toISOString().substring(0, 10); to output myDate into correct format?
You just output myFormattedDate and not myDate

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.