【发布时间】:2021-10-18 00:12:50
【问题描述】:
我目前正在为项目中的一个页面进行 yup 验证。我坚持以下几点: 用户选择事件的日期、时区和具体时间(他可以选择每十五分钟从 00:00 到 23:45 的时间)。现在,我需要验证选择的时间是否在未来。我真的很困惑:我应该首先将当前时刻转换为用户选择的时区,还是将用户的时刻转换为本地时区,或者其他什么。 当我对时区不小心时,这是我首先做的:
[keys.startTime]: yup
.number()
.required(errors.required)
.when(['date'], (date, schema: any) => {
const currentTimeInMinutes = moment().hours() * 60 + moment().minutes()
if (
moment(date).date() === moment().date() &&
moment(date).month() === moment().month() &&
moment(date).year() === moment().year()
)
return schema.min(currentTimeInMinutes, errors.pastTime)
}),
但后来我意识到我没有考虑选择的时区。 这里的开始时间表示从 00:00 到所选时间的分钟数,例如如果用户选择 00:15,则 startTime 将为 15。 提前致谢。
【问题讨论】:
标签: reactjs validation yup