Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation .isValid() doesn't work always #320

Closed
dakshshah96 opened this issue Aug 31, 2018 · 19 comments
Closed

Validation .isValid() doesn't work always #320

dakshshah96 opened this issue Aug 31, 2018 · 19 comments

Comments

@dakshshah96
Copy link

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')
@mixrich
Copy link

mixrich commented Oct 4, 2018

@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

@mixrich
Copy link

mixrich commented Oct 4, 2018

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

@prantlf
Copy link
Contributor

prantlf commented Oct 14, 2018

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.

@huberzeljko
Copy link

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

@YangAi
Copy link

YangAi commented Oct 24, 2019

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.

@ness-EE
Copy link

ness-EE commented Dec 21, 2020

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

@bitnbytesio
Copy link

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?

@padas2
Copy link

padas2 commented Mar 4, 2021

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 ?

@mateuszpigula
Copy link
Contributor

mateuszpigula commented Mar 4, 2021

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

@vamshi29292
Copy link

vamshi29292 commented Mar 4, 2021

@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.

@wmonecke
Copy link

wmonecke commented Jun 5, 2021

Why is this still closed? This issue cost me at least 4 hours because it is still broken.

@fragsalat
Copy link

fragsalat commented Oct 6, 2021

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.

@gamevnlc
Copy link

gamevnlc commented Mar 4, 2022

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

@NikhilNanjappa
Copy link

NikhilNanjappa commented Jun 28, 2022

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.

@RobAtRAC
Copy link

RobAtRAC commented Oct 7, 2022

That's still validating as true for me too

@aderchox
Copy link

aderchox commented Jan 20, 2023

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.

@ethanfann
Copy link

Why does dayjs require a special plugin just to validate if a month has a 31st day?

@VladyslavYe
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests