Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
968 views
in Technique[技术] by (71.8m points)

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

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 of manually updating the dates every time after they are retrieved, anyone been able to solve this easily? Or am I missing something?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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...
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...