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.

Kevin Bryan
  • 1,846
  • 2
  • 22
  • 45
  • Just format your date – Weedoze Apr 11 '17 at 08: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. – Shilly Apr 11 '17 at 08:37
  • 1
    It's because (I'm assuming) it's parsing it to a [JS Date object](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date) which also stores the time, the good news is you can format it to only show the date values – George Apr 11 '17 at 08:37
  • @George how? can you please show me an example of the code. – Kevin Bryan Apr 11 '17 at 08:38

1 Answers1

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

Community
  • 1
  • 1
Anthony Granger
  • 784
  • 9
  • 23