2012-08-24 151 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