Validation .isValid() doesn't work always
The .isValid() validation doesn't function correctly under the following condition:
- When the the day part is 31 for a month which didn't have 31 days.
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.
I'm just swapping over from Moment to DayJS. The unit tests I have in place to validate incorrect entries like 2000-02-31 by using isValid() pass with Moment and fail with DayJS
facing same issue, isValid() always returns true
const dayjs = require("dayjs")
console.log(dayjs('2021-15-35').isValid())
const dayjs = require("dayjs")
console.log(dayjs('2021-15-35', 'YYYY-MM-DD').isValid())
Expected: false Received: true
Any suggestion?
Turns out
dayjs('31/02/2021', 'DD/MM/YYYY', true).isValid()
can be used to check if passed date exists or not.
But not sure how... could somebody explain ?
console.log(dayjs("2021-15-35", "YYYY-MM-DD").isValid()); // true
console.log(dayjs("35/15/2021", "DD/MM/YYYY").isValid()); // false
So it is somehow related to the date format.
isValid method relies only on the native Date object implementation.
It returns invalid only if new Date(string).toString() returns Invalid Date
@mateuszpigula passing a third parameter true is checking if the date actually exists, unlike the native Date implementation.
dayjs("2021-15-35", "YYYY-MM-DD").isValid(); //true
dayjs("2021-15-35", "YYYY-MM-DD", true).isValid(); //false
I've tested a few cases including leap years, and this seems to work fine to check if the date actually exists.
Why is this still closed? This issue cost me at least 4 hours because it is still broken.
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
Such workaround only works for having DD and MM. Once you have non leading zero with D and M neither this approach nor the strict flag solves the issue. :(
Reason for this is the customParseFormat plugin parses the string date and passes it to new Date(Date.UTC(year, month, day)) and the js date implementation adds the overflowing days to the value.
like new Date(Date.UTC(2021, 1, 31)) results in 3rd March because 2021-02-29 + 4 days is the 3rd March.
A fix to this could be to add a check to the customFormatParser if the date.getMonth() equals the parsed month.
I get the same issue
dayjs('01/02, 20:00', 'MM/DD/YYYY, HH:mm', true).isValid()) // True
if I extends with plugin customParseFormat. The result is False
Any update on this ?
I still get incorrect results (v1.11.3)
dayjs('2001-2-29', 'YYYY-M-D').isValid() // returns true; expected false
dayjs('2001-02-29', 'YYYY-MM-DD').isValid() // returns true; expected false
// date becomes Mar 01 2001 which is why its considered valid
FYI - Adding customParseFormat plugin doesnt fix this.
That's still validating as true for me too
This is the very first thing I've tried with Day.js and it's failed. I've installed the customParseFormat module too (and I've done the extend).
console.log(dayjs("31-11-2022", "DD-MM-YYYY").isValid()); gives true.
Why does dayjs require a special plugin just to validate if a month has a 31st day?
Also, if you are having this problem check the parsing of your date:
dayjs('27.07.2023', 'DD.MM.YYYYY').format('DD.MM.YYYYY') // Invalid Date
If this occurs try adding an ext:
import customParseFormat from 'dayjs/plugin/customParseFormat';
dayjs.extend(customParseFormat);
dayjs('27.07.2023', 'DD.MM.YYYYY').format('DD.MM.YYYYY') // 27.07.2023
Hope this helped someone.