MySQL数据库时间戳记

MySQL数据库时间戳记

本文介绍了ISO 8601 MySQL数据库时间戳记:MySQL日期时间值不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误日志:

我使用这个pi我的 Node.js中的代码的代码

 如果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\')
/ pre>

我做错了吗?我刚从服务器中的表中使用 PHPMyAdmin 复制表。



非常感谢。 b $ b

解决方案

如:

您的日期文字'2012-08-24T17:29:11.683Z' code>不适合任何这些格式;建议您使用




  • 使用Node.js Date对象的方法(请确保MySQL连接的时区与Node .js的locale):

      if s instanceof Date 
    return s.toLocaleFormat(%Y-%m-% d%H:%M:%S)


  • 使用Node.js Date对象方法从UNIX时代起,以 1000 (从UNIX时代开始获得)获得毫秒中的时间值,并通过MySQL的功能。



Error log:

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.

解决方案

As stated in Date and Time Literals:

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.

这篇关于ISO 8601 MySQL数据库时间戳记:MySQL日期时间值不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 00:04