2012-08-24 152 views
5

错误日志:ISO 8601时间戳MySQL数据库:MySQL的不正确的时间值

{ [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] }

我使用这段代码在我Node.js

if s instanceof Date 
     return s.toISOString() 

并在数据库中更新它们。

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\') 

难道我做错了什么?我刚刚从服务器中的表中使用PHPMyAdmin复制了一张表。

非常感谢。

回答

9

正如Date and Time Literals说:

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 '[email protected]@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” .

您的日期文字的'2012-08-24T17:29:11.683Z'不适合任何这些格式;建议你要么—

  • 改用Node.js的Date对象的toLocaleFormat()方法(可以肯定的是,MySQL连接的时区相匹配的Node.js的地方的那个):

    if s instanceof Date 
         return s.toLocaleFormat("%Y-%m-%d %H:%M:%S") 
    
  • 使用Node.js Date对象的valueOf()方法获取毫秒的时间值自UNIX纪元以来,除以1000(自UNIX纪元以来得到),并通过My SQL的FROM_UNIXTIME()函数。

+0

它是否改变了不同版本的'MySQL'的?我确信在我复制表的服务器上运行正常。 –

+0

@ComboZhc:不是我的知识。也许PHPMyAdmin以这种方式格式化输出以显示,但格式不是有效的MySQL日期时间文字。如果需要,您可以使用['STR_TO_DATE()'](http://dev.mysql.com/doc//en/date-and-time-functions.html#function_str-to-date)执行转换。 – eggyal

+0

如果在使用毫秒和/或微秒设置日期时遇到问题,可以禁用mysql严格模式(set sql_mode ='') – momo

2

我发现此链接:

MySQL insert to DATETIME: is it safe to use ISO::8601 format?

似乎插入ISO8601时间戳是不是安全。这取决于MySQL的解析器。也许不同版本使用不同的方法。

Date.prototype.format = (format) -> 
    o = { 
    "(M+)" : this.getMonth()+1, 
    "(d+)" : this.getDate(), 
    "(h+)" : this.getHours(), 
    "(m+)" : this.getMinutes(), 
    "(s+)" : this.getSeconds(), 
    "(q+)" : Math.floor((this.getMonth()+3)/3), 
    "(S)" : this.getMilliseconds() 
    } 
    if /(y+)/.test(format) 
    format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)) 
    for k, v of o 
    if (new RegExp(k)).test(format) 
     format = format.replace(RegExp.$1, if RegExp.$1.length == 1 then o[k] else ('00'+ o[k]).substr((''+ o[k]).length)) 
    return format 

这片码可提供node.js与格式化能力Date