Knexjs返回mysql时间戳,datetime列作为Javascript Date对象

编程入门 行业动态 更新时间:2024-10-06 22:27:04
本文介绍了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对象

本文发布于:2023-10-23 02:33:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519482.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   时间   mysql   Knexjs   Date

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!