Search code examples
node.jsmysql-workbenchknex.js

SQL is returning an incorrect date


I'm using

  • MySQL Workbench
  • Node.js with Knex.js

My data in the SQL looks like this:

+--------------+----+--------------------+
|     date     | id | happinessIndicator |
+--------------+----+--------------------+
| '2019-10-20' |  1 | happy              |
| '2019-10-20' |  2 | happy              |
| '2019-10-20' |  3 | happy              |
| '2019-10-20' |  4 | happy              |
+--------------+----+--------------------+

That's my code to get the votes of the current day

exports.day = function (req, res) {
    const today = moment().format('YYYY-MM-DD');
    knex('votes')
        .where('date', today)
        .then(function (resp) {
            res.json({
                status: true,
                message: 'ok',
                data: resp
            })
        })
        .catch(function (error) {
            res.json(
                {
                    status: false,
                    msg: error.code
                }
            )
        })
};

but my result looks like this:

enter image description here

Why doesn't he return 2019-10-20 like there is stored?


Solution

  • this is not an incorrect date, this is UTC format.

    you can set your own timzone format in your connection object lilke this: knex({client,connection,useNullAsDefault: true,timezone: 'UTC',});