简体   繁体   English

如何使用Javascript在MYSQL中存储TIME数据类型?

[英]How to store in TIME data type in MYSQL using Javascript?

I am using SailsJS and MYSQL database.我正在使用 SailsJS 和 MYSQL 数据库。 I already read the documentation about the data type TIME so basically said that you can add as a literal value with the format HH:MM:SS as far as I can see in the sample you insert the a new data with:我已经阅读了有关数据类型TIME的文档,所以基本上说您可以添加格式为HH:MM:SS的文字值,就我在示例中看到的您插入新数据而言:

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

I also tried use momentjs parsing my string like moment(00:00:00, 'HH:mm:ss') according to the documentation you are able to have a value, but after reviewing in the console I noticed this:我还尝试使用 momentjs 解析我的字符串,例如moment(00:00:00, 'HH:mm:ss')根据您能够拥有值的文档,但是在控制台中查看后,我注意到了这一点:

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

So automatically create a date for the time that put on.因此,自动为穿上的时间创建一个日期。

I appreciate any help.我很感激任何帮助。

After research a bit more.稍微研究了一下之后。 I realized that the way you pass in mysql the time is just a string.我意识到你在 mysql 中传递时间的方式只是一个字符串。 So I change in my model from this:所以我改变了我的模型:

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

to this:对此:

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

And then you can store it successfully.然后你就可以成功存储它了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM