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

Invalid dates are parsed as valid #1238

Closed
manojkakarla opened this issue Nov 24, 2020 · 23 comments · Fixed by #1321
Closed

Invalid dates are parsed as valid #1238

manojkakarla opened this issue Nov 24, 2020 · 23 comments · Fixed by #1321
Labels

Comments

@manojkakarla
Copy link

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
@iamkun
Copy link
Owner

iamkun commented Nov 24, 2020

Yes, this is valid to js Date.

You can use strict parsing to get what you need. https://day.js.org/docs/en/parse/string-format

dayjs('1970-00-00', 'YYYY-MM-DD', true).isValid() // false 

@bornova
Copy link

bornova commented Dec 1, 2020

Does Day.js strict parsing validate time formats as well? Following returns true. Should it return false since date string is missing the time?

dayjs('12/3/2020', 'M/D/YYYY h:mm A', true).isValid() // true

@iamkun
Copy link
Owner

iamkun commented Dec 2, 2020

well, seems this is something we've never think about it.

this depends on how we define 'isValid', it should be a valid date-time? or valid and match the given format?

@bornova
Copy link

bornova commented Dec 2, 2020

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.

@Gabee01
Copy link

Gabee01 commented Dec 9, 2020

+1

@Gabee01
Copy link

Gabee01 commented Dec 9, 2020

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

@bornova
Copy link

bornova commented Dec 9, 2020

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

@Gabee01
Copy link

Gabee01 commented Dec 14, 2020

@bornova I'm now wondering what have I done wrong at the time. I tested the validation several times, before giving up on this. It really looks like its working on runkit..

@iamkun
Copy link
Owner

iamkun commented Dec 14, 2020

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

@Gabee01
Copy link

Gabee01 commented Dec 14, 2020

@iamkun CustomParseFormat was really missing. thanks!

@bornova
Copy link

bornova commented Dec 14, 2020

@iamkun My issue still remains:
https://runkit.com/5fd79efc6abad7001a703bb0/5fd79f0342c42e001ad68c00

Any plans to address this in the near future? I had to switch back to Moment.js for the time being. Thanks for all your effort in this library!

@iamkun
Copy link
Owner

iamkun commented Jan 7, 2021

@bornova fixed in #1321

@iamkun
Copy link
Owner

iamkun commented Jan 9, 2021

🎉 This issue has been resolved in version 1.10.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

@iamkun iamkun added the released label Jan 9, 2021
@ghiscoding
Copy link

ghiscoding commented Feb 4, 2021

@iamkun I think you should reopen

This doesn't seem to be fixed, I want to migrate my lib from MomentJS to DayJS, so I'm fairly new to it but this is a bit of blocker to migrate. I expect the following lines to be both invalid and not be parseable/formattable

// with version 1.10.4
console.log(dayjs('1900-02-33', 'YYYY-MM-DD', true).isValid())  // true but should be false 33 is not a valid date
console.log(dayjs('1900-02-33', 'MM/DD/YYYY', true).isValid())  // true but again should be false 33 is not valid and it also shouldn't be parseable 

// trying to format them also works but changes the date completely
console.log(dayjs('1900-02-33', 'YYYY-MM-DD', true).format('MM/DD/YYYY'))  // "03/05/1900"
console.log(dayjs('1900-02-33', 'MM/DD/YYYY', true).format('MM/DD/YYYY'))  // "03/05/1900"

If I take the same code and use MomentJS it's throwing "Invalid date" (even without using strict) and I rely on that error message

EDIT

I read the CustomParseFormat after and now yes now it works. So you can forget my comment, thanks

@rrdlpl
Copy link

rrdlpl commented Feb 17, 2021

This should be reopened it's still happening for me

var dayjs = require("dayjs")
require('dayjs/locale/en')



var datestr= '20201312'

console.log(dayjs(datestr, 'YYYYMMDD', 'en', true).isValid()); // Returns true. 13 is not a valid month.

https://runkit.com/embed/1bnw0lmx5yv2

@ghiscoding
Copy link

@rrdlpl I think you did the same mistake as I did which is to forget adding CustomParseFormat for validating dates

@rrdlpl
Copy link

rrdlpl commented Feb 17, 2021

@ghiscoding I don;t understand what you meant. Could you please update my fiddle?

@ghiscoding
Copy link

@rrdlpl read this comment

@rrdlpl
Copy link

rrdlpl commented Feb 18, 2021

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()); 

@wmonecke
Copy link

wmonecke commented Jun 5, 2021

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

@glendell03
Copy link

glendell03 commented Jun 12, 2021

I was filtering my db and when I filter slug text "cajina-06-08-2021" it says true

image

@NikhilNanjappa
Copy link

NikhilNanjappa commented Jun 28, 2022

Extending customParseFormat does not fix this for me

I still get incorrect results (v1.11.3)

const customParseFormat = require('dayjs/plugin/customParseFormat')
dayjs.extend(customParseFormat)

dayjs('2001-2-29', 'YYYY-M-D', true).isValid() // returns true; expected false

@sensetive5
Copy link

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

Successfully merging a pull request may close this issue.

10 participants