【问题标题】:how to validate if entered date is of today or of future using yup如何使用 yup 验证输入的日期是今天还是未来
【发布时间】:2021-10-15 22:55:16
【问题描述】:

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

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;
    }),

【问题讨论】:

    标签: javascript reactjs yup


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 2023-04-01
      • 2021-11-28
      相关资源
      最近更新 更多