The .isValid() validation doesn't function correctly under the following condition:
Some instances where .isValid() returns true for a date that is invalid:
dayjs().isValid('1995-02-31')dayjs().isValid('1995-03-31')dayjs().isValid('1995-04-31')dayjs().isValid('1995-06-31')dayjs().isValid('1995-09-31')dayjs().isValid('1995-11-31')@dakshshah96
.isValid() method has't arguments and passed value is discarded like if you call dayjs().isValid(). And you know, that dayjs() return current date which is always valid
Moreover, isValid doesn't validate if passed date is exists, it's just validate that date was parsed correctly. So, if you pass dayjs('2018-11-41').isValid() the date will be parsed as 2018-12-10 and isValid will return true anyway
You should not overestimate the quality of such validation. You will be able to catch nonsense like MM=15, DD=42 or DD=30 in February, if the user exchanges values of DD with MM, or types something totally wrong. However, you won't be able to ensure, that the whole date - particularly DD and MM - will be set, as the user intended from an input like 02/05/2018. Was it May 2 or February 2? The user will still have to be aware of the actual format like DD/MM/YYYY. The date picker control usually helps better with ensuring that, than a robuster parser.
Is this an option to validate if date exist?
function validate(date, format) {
return dayjs(date, format).format(format) === date;
}
validate('2019/02/31', 'YYYY/MM/DD') // false
Is this an option to validate if date exist?
function validate(date, format) { return dayjs(date, format).format(format) === date; } validate('2019/02/31', 'YYYY/MM/DD') // false
Thanks! It's very helpful.
Most helpful comment
Is this an option to validate if date exist?