search

Home  >  Q&A  >  body text

node.js - 使用sequelize查询出来的createdAt字段格式问题

使用sequelize查询出来的createdAt字段,是“Tue Mar 14 2017 16:02:21 GMT+0800 (中国标准时间)”这样的字符串。怎样恢复原有格式或者进行自定义的格式化?

ringa_leeringa_lee2784 days ago882

reply all(2)I'll reply

  • 迷茫

    迷茫2017-04-17 16:21:53

    new Date("Tue Mar 14 2017 16:02:21 GMT+0800 (China Standard Time)").getTime()

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 16:21:53

    • What is your original format? YYYY-MM-DD HH:mm:ss ???

    const model = sequelize.define('model', {//columns specifications
      createdAt: {
          type : TYPE.DATE,
          allowNull : false,
          get() {
            const createAt = this.getDataValue('createAt')
            // createAt is an instance of date, you could operate it with moment library
            // to get ur format
            return createAt
          },
          set(createAt) {
            return this.setDataValue('createAt',createAt)
          }
        }
     }, {
      // options
      timestamps: true // the createdAt and updatedAt will be stored by YYYY-MM-DD HH:mm:ss
    });
    • If all your CRUD operations on the database are done through sequelize, there should be no problem

    reply
    0
  • Cancelreply