Javascript 如何将瞬间 JS 时间插入 MySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26047755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:14:46  来源:igfitidea点击:

How to insert moment JS time into MySQL

javascriptphpmysqlnode.jsmomentjs

提问by user1529412

//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 中的列是“日期时间”类型。

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

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

回答by DontVoteMeDown

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

出现此结果是因为您正在使用该toISOString()方法并且它不是插入到您的DATETIME列中的有效格式。正如文档指出的那样,正确的格式可能YYYY-MM-DD HH:MM:SS(我认为这取决于 MySQL 配置,但这是默认值)。

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

所以你应该尝试像这样使用 moment 的format()方法:

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.myTimeis, but if its a moment object too, you can change the first format()method and remove the second one.

事实上,我不知道是什么data.myTime,但如果它也是一个 moment 对象,您可以更改第一个format()方法并删除第二个方法。

回答by Sheraf

DontVoteMeDown answeris right, except that the minutes 'mm'and seconds 'ss'need to be in lowercase, otherwise a wrong value is returned:

DontVoteMeDown 答案是正确的,只是'mm'分秒'ss'需要小写,否则返回错误值:

myDate =  moment(new Date()).format("YYYY-MM-DD HH:mm:ss");

Also, you should check the value sent by javascript before to do your SQL query, in PHP:

此外,您应该在使用 PHP 执行 SQL 查询之前检查 javascript 发送的值:

DateTime::createFromFormat('Y-m-d H:i:s', $myDate);

Will return falseif the date is misformatted.

false如果日期格式错误,将返回。

回答by Yani

Here's a function extension to format it into the MySQL DateTime format

这是将其格式化为 MySQL DateTime 格式的函数扩展

moment.prototype.toMySqlDateTime = function () {
    return this.format('YYYY-MM-DD HH:mm:ss');
};

You'll be able to do: moment().toMySqlDateTime();

你将能够做到: moment().toMySqlDateTime();

回答by jlizanab

(I'm working with Node.js, Express and MySql and this has worked for me:)

(我正在使用 Node.js、Express 和 MySql,这对我有用:)

var momentDate=new moment('2018-08-31T20:13:00.000Z');
var readyToInsert=momentDate.format("YYYY-MM-DD HH:mm:ss");

回答by Salar

To generate mysql format datetime you can use this:

要生成 mysql 格式的日期时间,您可以使用:

var moment = require('moment');
let currentTimestamp = moment().unix();//in seconds
let currentDatetime = moment(currentTimestamp*1000).format("YYYY-MM-DD HH:mm:ss");//mysql datetime.
currentTimestamp = moment(currentDatetime).valueOf();//current timestamp in milliseconds

回答by Ashutosh Jha

moment().format("YYYY-MM-DD HH:mm:ss")

回答by Carlos

I think this is the correct format. Otherwise you get the months inserted in your minutes, and you also get seconds above 60.

我认为这是正确的格式。否则,您会在分钟中插入月份,并且您还会得到 60 秒以上的秒数。

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

Tested in Chrome with Moment.js 2.9.0

在 Chrome 中使用 Moment.js 2.9.0 进行测试