【问题标题】:MySQL - ER_TRUNCATED_WRONG_VALUE: Incorrect datetime value, on a Timestamp columnMySQL - ER_TRUNCATED_WRONG_VALUE:时间戳列上的日期时间值不正确
【发布时间】:2017-11-19 14:02:48
【问题描述】:

我有一个定义为 TIMESTAMP 的 MYSQL 列。

每当我使用 javascript new Date() 创建一行时,该值都可以正常存储而不会出现问题。

但是,当我想更新该值时,在该列上发送相同的 new Date(),我得到一个错误

Error: ER_TRUNCATED_WRONG_VALUE: Incorrect datetime value: '2017-06-16T17:35:34.377Z' for column 'last_access'.

我可以将整个事情管理为 Datetime 并相应地格式化日期以解决错误,但是我想了解错误发生的原因。

【问题讨论】:

    标签: mysql datetime error-handling timestamp


    【解决方案1】:

    MariaDB 的日期格式必须类似于“YYYY-MM-DD HH:MM:SS”。

    如果你想用 JS 插入一个日期,试着自己做一个字符串。

    const newDate: Date = new Date();
    const dd = newDate.getDate();
    const number = newDate.getMonth() + 1; // month start at 0, we have to add 1.
    const yyyy = newDate.getUTCFullYear();
    
    const insertString = `${yyyy}-${mm}-${dd}`;
    

    【讨论】:

      猜你喜欢
      • 2017-11-02
      • 1970-01-01
      • 2012-08-20
      • 2020-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多