如何将时间JS时间插入MySQL [英] How to insert moment JS time into MySQL

查看:115
本文介绍了如何将时间JS时间插入MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//In Node/JS
myDate = moment(data.myTime.format('YYYY/MM/DD HH:MM:SS')).toISOString();
//myDate shows '2014-09-24T04:09:00.000Z'

Insert INTO (dateColumn..) Values(myDate)...

这是我插入后得到的错误,Mysql中的note列是datetime类型。

This is the error I get after inserting, note column in Mysql is a "datetime" type.


MySQL错误:: {[错误:ER_TRUNCATED_WRONG_VALUE:日期时间值不正确:'2014-09- 24T04:09:00.000Z'对于第1行的'_dateColumn'列
代码:'ER_TRUNCATED_WRONG_VALUE',

MySQL Error:: { [Error: ER_TRUNCATED_WRONG_VALUE: Incorrect datetime value: '2014-09- 24T04:09:00.000Z' for column '_dateColumn' at row 1] code: 'ER_TRUNCATED_WRONG_VALUE',


推荐答案

这个结果的发生是因为你正在使用< a href =http://momentjs.com/docs/#/displaying/as-iso-string/> toISOString() 方法,它是不是插入 DATETIME 列的有效格式。正确的格式可能 YYYY-MM-DD HH:MM:SS (我认为这取决于MySQL配置,但这是默认设置)正如>文档所指出的那样。

This result happens because you are using the toISOString() method and it is not a valid format to insert into your DATETIME column. The correct format probably is YYYY-MM-DD HH:MM:SS(I think it depends on MySQL configuration, but this is the default) as the docs points out.

所以你应该尝试使用时刻的格式()这样的方法:

So you should try using moment's format() method like this:

myDate =  moment(data.myTime.format('YYYY/MM/DD HH:mm:ss')).format("YYYY-MM-DD HH:mm:ss");

事实上,我不知道 data.myTime 是,但如果它也是一个时刻对象,你可以改变第一个 format()方法并删除第二个。

In fact, I don't know what data.myTime is, but if its a moment object too, you can change the first format() method and remove the second one.

这篇关于如何将时间JS时间插入MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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