dayjs: Invalid dates are parsed as valid

Invalid dates are parsed resulting in incorrect values e.g, const dt = dayjs(‘1993-51-11’) dt.isValid() returns true and dt.format('YYYY-MM-DD') returns 1997-03-11 const dt = dayjs(‘1993-51-41’) dt.isValid() returns true and dt.format('YYYY-MM-DD') returns 1997-04-10 infact dayjs('1993-08-21', 'YYYY/MM/DD') returns true for isValid()

isValid() should return false for all these

Information

  • Day.js Version: v1.9.6
  • OS: macOS 11 BigSur
  • Browser: chrome 86
  • Time zone: GMT

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 23 (5 by maintainers)

Most upvoted comments

Do remember this dependent on CustomParseFormat plugin to work https://day.js.org/docs/en/parse/string-format

IMHO, it should match the given format. Btw, I also found the following:

dayjs('1/30/2020', 'M/D/YYYY', true).isValid() // true but dayjs('30/1/2020 10:59 PM', 'D/M/YYYY h:mm A', true).isValid() // false

Both should be true.

For the people that still are struggling with this. As the other people mentioned you need CustomParseFormat

const dayjs = require("dayjs")
require('dayjs/locale/en')
const customParseFormat = require('dayjs/plugin/customParseFormat');
dayjs.extend(customParseFormat);

const datestr= '20201312'

console.log(dayjs(datestr, 'YYYYMMDD', 'en', true).isValid()); 

using the strict boolean does not work here as well. dayjs('00-00-1990', 'DD-MM-YYYY', true).isValid() // true

@Gabee01 Your example returns the correct value (false) on my end: https://runkit.com/5fd149b2b4b41f001d9b4bf4/5fd149f5b4b41f001d9b4c26

Why is this issue closed? I don’t understand. I guess I am going back to moment.

using the strict boolean does not work here as well. dayjs('00-00-1990', 'DD-MM-YYYY', true).isValid() // true edit: As @bornova script shows, it really works. Maybe my mistake were somewhere else