mysql iso 时间,ISO 8601 MySQL数据库时间戳记:MySQL日期时间值不正确

Error log:

{ [Error: Incorrect datetime value: '2012-08-24T17:29:11.683Z' for

column 'robot _refreshed_at' at row 1] number: 1292, sqlStateMarker:

'#', sqlState: '22007', message: 'Incorrect datetime value:

\'2012-08-24T17:29:11.683Z\' for column \' robot_refreshed_at\' at row

1', sql: 'INSERT INTO users

(id,name,count_moments,count_likes,count_followers,rob

ot_refreshed_at,robot_count_followers) VALUES

(\'1834084\',\'NNNyingzi\',\'5\',\

'0\',\'0\',\'2012-08-24T17:29:11.683Z\',\'0\')', setMaxListeners:

[Function], emit: [Function], addListener: [Function], on: [Function],

once: [Function], removeListener: [Function], removeAllListeners:

[Function], listeners: [Function] }

I use this piece of code in my Node.js

if s instanceof Date

return s.toISOString()

and updated them in database.

The SQL insert expression follows:

INSERT INTO users (id,name,count_moments,count_likes,count_followers,rob ot_refreshed_at,robot_count_followers) VALUES (\'1834084\',\'NNNyingzi\',\'5\',\ '0\',\'0\',\'2012-08-24T17:29:11.683Z\',\'0\')

Am I doing anything wrong? I just copied a table using PHPMyAdmin from a table in server.

Thanks a lot.

解决方案MySQL recognizes DATETIME and TIMESTAMP values in these formats:

As a string in either 'YYYY-MM-DD HH:MM:SS' or 'YY-MM-DD HH:MM:SS' format. A “relaxed” syntax is permitted here, too: Any punctuation character may be used as the delimiter between date parts or time parts. For example, '2012-12-31 11:30:45', '2012^12^31 11+30+45', '2012/12/31 11*30*45', and '2012@12@31 11^30^45' are equivalent.

As a string with no delimiters in either 'YYYYMMDDHHMMSS' or 'YYMMDDHHMMSS' format, provided that the string makes sense as a date. For example, '20070523091528' and '070523091528' are interpreted as '2007-05-23 09:15:28', but '071122129015' is illegal (it has a nonsensical minute part) and becomes '0000-00-00 00:00:00'.

As a number in either YYYYMMDDHHMMSS or YYMMDDHHMMSS format, provided that the number makes sense as a date. For example, 19830905132800 and 830905132800 are interpreted as '1983-09-05 13:28:00'.

A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision. Although this fractional part is recognized, it is discarded from values stored into DATETIME or TIMESTAMP columns. For information about fractional seconds support in MySQL, see Section 11.3.6, “Fractional Seconds in Time Values”.

Your date literal of '2012-08-24T17:29:11.683Z' does not fit any of these formats; suggest you either—

use instead the Node.js Date object's toLocaleFormat() method (be sure that the timezone of the MySQL connection matches that of Node.js's locale):

if s instanceof Date

return s.toLocaleFormat("%Y-%m-%d %H:%M:%S")

use the Node.js Date object's valueOf() method to obtain the time's value in milliseconds since the UNIX epoch, divide by 1000 (to get seconds since the UNIX epoch) and pass through MySQL's FROM_UNIXTIME() function.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值