首页 > 解决方案 > 如何使用 yup 验证输入的日期是今天还是未来

问题描述

这适用于除同一天以外的所有情况,

Yup.date()
    .required(“Start Date is required”)
    .test(“startDate”, “Start Date can not be a passed Date”, function (date) {
      const cutoff = new Date();
      const selectedDate = date;
      return selectedDate >= cutoff;
    }),

标签: javascriptreactjsyup

解决方案


与其现在使用获取日期/时间new Date(),不如将其时间设置为今天的开始:

const cutoff = new Date();
cutoff.setHours(0, 0, 0, 0);

推荐阅读