Is there a simple way to make Sequelize return it's date/time fields in a particular format?

后端 未结 4 1292
攒了一身酷
攒了一身酷 2020-12-16 00:44

We need to have sequelize return dates in a particular format, not the default one. As far as I can tell, there is no way to set that up in options, or any other way. Short

4条回答
  •  青春惊慌失措
    2020-12-16 01:12

    You can, use the Sequelize fn method. From the API Reference, the fn function will help create an object representing a SQL function in your query.

    For example:

    model.findAll({
      attributes: [
          'id',
          [sequelize.fn('date_format', sequelize.col('date_col'), '%Y-%m-%d'), 'date_col_formed']
      ]})
      .then(function(result) {
        console.log(result);
      });
    

    Will return data values:

    [
      {"id": 1, "date_col_formed": "2014-01-01"},
      {"id": 2, "date_col_formed": "2014-01-02"}
       // and so on...
    ]
    

提交回复
热议问题