使用 Joi 验证日期而不将其转换为 utc 格式

Validate date using Joi without convert it to utc format

有没有一种方法可以使用@joi/date 来验证日期而不将日期更改为 UTC 格式。

我试图在将日期发送到数据库之前验证日期

我试过的

const Joi = require('joi').extend(require('@joi/date'));
...
    const schema = Joi.object({
        f_title: Joi.string().required(),
        date: Joi.date().format('YYYY-MM-DD').required(),
    });

但问题是 2000-05-15 将始终转换为 2000-05-15T21:00:00.000Z 并且数据库将生成不正确的日期值。

谢谢:)

一个可能的解决方法是从用户发送的日期开始一个新的日期对象,接下来,你可以获得完整的年月日......如果你使用这种方式,你会得到无论用户使用何种浏览器,此格式的日期。

const Joi = require('joi').extend(require('@joi/date'));
...
    const schema = Joi.object({
        f_title: Joi.string().required(),
        dateVarFromUser: Joi.date().format('YYYY-MM-DD').required(),
    });
###
// Rest of your code
###

Before sending the request to the DB, add the following

const datObj = new Date(dateVarFromUser);
dateVarFromUser = datObj.getFullYear() + '-' + (datObj.getMonth()+1) + '-' + datObj.getDate()

###
// Then, you could send it to the SQL handler/function
###