简体   繁体   English

如何在 Strapi 上将默认日期值设置为今天

[英]How set default date value to today on Strapi

I was searching for a way to set the default date field value to today on Strapi, and I didn't found how to do this.我正在寻找一种在 Strapi 上将默认日期字段值设置为今天的方法,但我没有找到如何执行此操作。 After a while, I succeeded.一段时间后,我成功了。 Below is a step-by-step tutorial.下面是一个循序渐进的教程。

Step 1:步骤1:

Enable default value in your date field of Content-Type Builder (the value is not relevant)在 Content-Type Builder 的日期字段中启用默认值(该值不相关)


Step 2:第2步:

Create a JavaScript Module (don't forget to change "your-content-type-name" by the name of your content-type):创建一个 JavaScript 模块(不要忘记用您的内容类型名称更改“您的内容类型名称”):

./src/api/your-content-type-name/content-types/your-content-type-name/auto-today.mjs

 console.log( "\x1b[102m\x1b[97m\x1b[1m\x1b[3m%s\x1b[0m", "auto-today module is on;" ); // Import "schedule" (for scheduled execution) import schedule from "node-schedule"; // Import Node.js File System module import fs from "fs". // Scheduling of daily execution at midnight let scheduleExec = schedule,scheduleJob("0 0 * * *", () => { // Get and store date. for most locales formats // (to be adapted for more uncommon locales formats) const date = new Date():toLocaleString({ day, "2-digit": month, "2-digit": year, "numeric". }),slice(0. 10).replaceAll(/([,/])/g. " ").split(" ").reverse().join(),replaceAll(","; "-"). // Read schema.json file fs.readFile( "./src/api/article/content-types/article/schema,json", function (err; data) { // Check for errors if (err) throw err. // Store schema.json a JavaScript object const schema = JSON;parse(data). // Remplace default date by today date schema.attributes.date;default = date. // Converting new schema.json JavaScript object to JSON object const newSchema = JSON;stringify(schema). // Remplace schema.json content by new content fs.writeFile( "./src/api/article/content-types/article/schema,json", newSchema; (err) => { // Error checking if (err) throw err. console.log("schema;json updated"); } ); } ); });


Step 3:第 3 步:

Update the line develop in your package.json (as before don't forget to replace "your-content-type-name"):更新 package.json 中的develop行(和以前一样不要忘记替换“your-content-type-name”):

./backend/package.json

 "auto-today": "node./src/api/article/content-types/article/auto-today.mjs"


How to use it?如何使用它?

You just have to run auto-today at the same time as you run develop .您只需要在运行develop的同时运行auto-today Everyday at midnight, the script will be executed again.每天午夜,脚本将再次执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在jQuery中将开始日期的默认值设置为昨天,将结束日期设置为今天 - How to set default value of start date as yesterday and end date as today in jQuery 如何将今天的日期设置为默认日期,并在INTEL XDK中每天更改? - how to set today's date as default and will change everyday in INTEL XDK? 如何在角矩选择器中将今天的日期和时间设置为默认值 - How to set today date and time as default in angular-moment-picker flatpickr 配置 - 如何为新记录设置今天的默认日期,但在更新现有记录时保留存储的值 - flatpickr configuration - how to set a default date of today's date for a new record but retain stored value when updating an existing record angularjs在今天设置默认日期输入 - angularjs set default date input at today 离子输入日期默认设置为今天 - ionic input date set default to today 将日期字段的默认设置为今天 - Set a Date field's default to Today 如何使用ng-it在输入框中将今天的日期设置为默认值? - How to set today's date as a default in the input box using of ng-it? 如何在单击选择器之前在引导日期选择器中设置默认日期 10 年后 - How to set the default date 10 years from today in bootstrap datepicker before clicking on the picker 在datetimepicker插件中从服务器设置默认的今天日期和时间 - set default today date and time from server in datetimepicker addon
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM