Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert moment JS time into 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)... 

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

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',

like image 709
user1529412 Avatar asked Sep 25 '14 20:09

user1529412


People also ask

How do you add time to a moment?

This is a pretty robust function for adding time to an existing moment. To add time, pass the key of what time you want to add, and the amount you want to add. moment(). add(7, 'days');


1 Answers

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"); 

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.

like image 94
DontVoteMeDown Avatar answered Sep 22 '22 07:09

DontVoteMeDown