如何使用打字稿验证 angular ionic 5 中的日期

how to validating the date in angular ionic5 using typescript

我已经尝试了很多解决方案,但无法通过用户输入日期来验证当前日期 在函数参数中从用户传递的日期如何执行验证 如何验证日期

isToday(date) {
    const today = new Date()
    return date.getDate() === today.getDate() &&
        date.getMonth() === today.getMonth() &&
        date.getFullYear() === today.getFullYear();
};

isToday(2020-09-07)

以上代码无法运行任何人都可以帮助

提前致谢

2020-09-07 不是 Date 对象,您需要使用 new Date('2020-09-07') 调用 isToday

function isToday(date) {
    const today = new Date()
    return date.getDate() === today.getDate() &&
        date.getMonth() === today.getMonth() &&
        date.getFullYear() === today.getFullYear();
};

console.log(isToday(new Date("2020-09-10")))
  1. console.log 只是为了知道它的结果。
  2. 2020-09-07 不是日期。你必须做 Date("2020-09-07")

此外,由于您还添加了打字稿作为标签,因此您应该使用类型:

function isToday(date : Date) {
    const today = new Date()
    return date.getDate() === today.getDate() &&
        date.getMonth() === today.getMonth() &&
        date.getFullYear() === today.getFullYear();
};

console.log(isToday(new Date("2020-09-10")))