Check if date is a valid one

前端 未结 9 655
再見小時候
再見小時候 2020-12-13 11:52

Following is the scenario:

I have a String date and a date format which is different. Ex.:
date: 2016-10-19
dateFormat: \"DD-MM-YYYY\".

9条回答
  •  星月不相逢
    2020-12-13 12:06

    If the date is valid then the getTime() will always be equal to itself.

    var date = new Date('2019-12-12');
    if(date.getTime() - date.getTime() === 0) {
        console.log('Date is valid');
    } else {
        console.log('Date is invalid');
    }
    

提交回复
热议问题