dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

Validation .isValid() doesn't work always

Open dakshshah96 opened this issue 8 years ago • 19 comments

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 avatar Aug 31 '18 20:08 dakshshah96

@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 avatar Oct 04 '18 09:10 mixrich

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

mixrich avatar Oct 04 '18 10:10 mixrich

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.

prantlf avatar Oct 14 '18 11:10 prantlf

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

huberzeljko avatar Oct 03 '19 10:10 huberzeljko

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.

YangAi avatar Oct 24 '19 15:10 YangAi

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

ness-EE avatar Dec 21 '20 15:12 ness-EE

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?

bitnbytesio avatar Feb 15 '21 07:02 bitnbytesio

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 ?

padas2 avatar Mar 04 '21 06:03 padas2

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 avatar Mar 04 '21 09:03 mateuszpigula

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

vamshi29292 avatar Mar 04 '21 12:03 vamshi29292

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

wmonecke avatar Jun 05 '21 19:06 wmonecke

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.

fragsalat avatar Oct 06 '21 10:10 fragsalat

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

gamevnlc avatar Mar 04 '22 09:03 gamevnlc

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.

NikhilNanjappa avatar Jun 28 '22 14:06 NikhilNanjappa

That's still validating as true for me too

PETTER-RAC avatar Oct 07 '22 09:10 PETTER-RAC

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.

aderchox avatar Jan 20 '23 19:01 aderchox

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

ethanfann avatar Jun 21 '23 19:06 ethanfann

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.

VladyslavYe avatar Jul 27 '23 09:07 VladyslavYe