Knexjs返回mysql时间戳,datetime列作为Javascript Date对象 [英] Knexjs returning mysql timestamp, datetime columns as Javascript Date object

查看:443
本文介绍了Knexjs返回mysql时间戳,datetime列作为Javascript Date对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用knexjs,我以YYYY-MM-DD HH:mm:ss例如2017-07-14 15:00:00的格式插入数据,并且在保存数据后保存时,将datetime列值作为javasript Date对象返回. 我想以YYYY-MM-DD HH:mm:ss格式返回那些对象,但以YYYY-MM-DDTHH:mm:ss.000Z格式返回,例如2017-06-23T06:44:44.000Z. 我通过迭代和手动转换它们来返回它们.我想知道是否还有另一种方式可以做到,例如在mysql驱动程序或knexjs配置中.目前,我的knexjs配置是这个.

i am using knexjs i insert data in the format YYYY-MM-DD HH:mm:ss e.g 2017-07-14 15:00:00 and after saving when the data is fetched the datetime column values are returned as javasript Date object. i want to return those object in the format YYYY-MM-DD HH:mm:ss but it returning in the format YYYY-MM-DDTHH:mm:ss.000Z e.g 2017-06-23T06:44:44.000Z. i am returning them by iterating and converting them manually. i was wondering if there is another way to do it like in mysql driver or knexjs configuration. currently my knexjs configuration is this.

 var connection = require('knex')({
            client: 'mysql',
            connection: {
                host: db.host,
                user: db.user,
                password: db.password,
                database: db.database,
                timezone: 'UTC'
            }
       });

推荐答案

使用以下方法更改连接对象:

Change your connection object with this:

var connection = require('knex')({
        client: 'mysql',
        connection: {
            host: db.host,
            user: db.user,
            password: db.password,
            database: db.database,
            timezone: 'UTC',
            dateStrings: true
        }
   });

这篇关于Knexjs返回mysql时间戳,datetime列作为Javascript Date对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆