How can I set the default value for a date to be today?

System Information
  • Strapi Version: 3.2.4
  • Operating System: Linux 5.16.0
  • Database: PostgreSQL 13.4
  • Node Version: 17.3.0
  • NPM Version: 8.3.0
  • Yarn Version: 1.22.17

Actually the whole question is contained in the title of the topic. It is necessary that when creating a post, it should have the standard value of the date field as today. How can i do it?


Okey. I resolved it using afterCreate in lifecyrcle hook. Topic can be closed.

1 Like

Hi! Can you describe in more detail solve of this problem?

2 Likes

How is this a solution? What approach did you take to the lifecycle?

maybe it’s too late to answer, but you need to create a lifecycles.js file in the /content-type/{collection_name}/ folder and add the following code for DateTime field:

module.exports = {
  async beforeCreate(event) {
    const {data} = event.params
    data.your_date_field = new Date();
  }
}

For date field use: new Date().toISOString().substring(0, 10);