【问题标题】:how to validating the date in angular ionic5 using typescript如何使用打字稿验证角度离子5中的日期
【发布时间】:2020-12-29 00:55:14
【问题描述】:

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

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

isToday(2020-09-07)

上面的代码不起作用,谁能帮忙

提前致谢

【问题讨论】:

    标签: javascript angular typescript ionic-framework ionic5


    【解决方案1】:
    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")

    此外,由于您还添加了 typescript 作为标签,那么您应该使用类型:

    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")))
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-13
        • 1970-01-01
        • 2020-11-18
        • 2017-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多