【问题标题】:How to store in TIME data type in MYSQL using Javascript?如何使用 Javascript 在 MYSQL 中存储 TIME 数据类型?
【发布时间】:2020-06-15 03:53:37
【问题描述】:

我正在使用 SailsJS 和 MYSQL 数据库。 我已经阅读了有关数据类型 TIME 的文档,所以基本上说您可以添加格式为 HH:MM:SS 的文字值,据我在示例中看到的您插入新数据:

INSERT INTO tests(name,start_at,end_at)
VALUES('Test 2','083000','101500');

我还尝试使用 momentjs 解析我的字符串,如 moment(00:00:00, 'HH:mm:ss') 根据文档你可以有一个值,但在控制台中查看后我注意到了这一点:

moment("2020-03-02T00:00:00.000")

所以自动为穿上的时间创建一个日期。

感谢您的帮助。

【问题讨论】:

    标签: javascript mysql sails.js momentjs


    【解决方案1】:

    经过一番研究。我意识到你在mysql中传递时间的方式只是一个字符串。所以我改变了我的模型:

    module.exports = {
      attributes: {
        name: { type: 'string', required: true },
        start_at: { type: 'time'},
        end_at: { type: 'time'}
      },
    };
    

    到这里:

    module.exports = {
      attributes: {
        name: { type: 'string', required: true },
        start_at: { type: 'string'},
        end_at: { type: 'string'}
      },
    };
    

    然后就可以存储成功了。

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 1970-01-01
      • 2015-08-25
      • 2012-02-27
      • 2012-07-31
      • 1970-01-01
      • 2017-01-24
      • 2013-06-24
      • 2016-10-27
      相关资源
      最近更新 更多