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

后端 未结 4 1269
攒了一身酷
攒了一身酷 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...
    ]
    
    0 讨论(0)
  • 2020-12-16 01:18

    21/06/2019

    A little bit late but providing an update.

    Sequelize is a powerful ORM (I am not saying is the best solution out there) but has a very bad documentation.

    Anyway if you want to have this configured in your models one way apart from having to repeat this across your queries as other responses state you could be doing:

         const Test = sequelize.define('test', {
                    // attributes
                    name: {
                        type: DataType.STRING,
                        allowNull: false
                    },
                    createdAt: {
                        type: DataType.DATE,
         //note here this is the guy that you are looking for                   
                      get() {
                            return moment(this.getDataValue('createdAt')).format('DD/MM/YYYY h:mm:ss');
                        }
                    },
                    updatedAt: {
                        type: DataType.DATE,
                        get() {
                            return moment(this.getDataValue('updatedAt')).format('DD/MM/YYYY h:mm:ss');
                        }
                    }
    

    If you are using dates to provide info as: last updated, first login, last login. This is the way to go.

    The function get returns the new formatted element so it should not be restricted to dates either! Just bear in mind that this will slow your query so use cautiously. ;)

    more info (i know i know but docs is the name of the game): http://docs.sequelizejs.com/manual/models-definition.html#getters--amp--setters

    0 讨论(0)
  • 2020-12-16 01:23

    you can define custom instance methods getDate/setDate which would translate date between sequelize internal representation and desired format like so http://sequelize.readthedocs.org/en/latest/docs/models-definition/index.html (see 'Expansion of models' section)

    0 讨论(0)
  • 2020-12-16 01:24

    In case of Model that we create using Sequelize CLI try something like this

               var sequelize= require('../models');
        model.findAll({
                attributes: [
                           'id',
                           'title'
               [sequelize.Sequelize.fn('date_format', sequelize.Sequelize.col('col_name'), '%d %b %y'), 'col_name']
                                ]}.then(function(result))
                                 { // dateformate=04 Nov 2017
    console.log(result)
    }
    

    visit this link for formate

    0 讨论(0)
提交回复
热议问题