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

dayjs(item).isValid() is not validate item.values #1543

Open
admin00001 opened this issue Jun 21, 2021 · 4 comments
Open

dayjs(item).isValid() is not validate item.values #1543

admin00001 opened this issue Jun 21, 2021 · 4 comments

Comments

@admin00001
Copy link

Describe the bug
in moment:

const o = {values: ['2017-06-17']}
moment(o).isValid() === true

but in dayjs

const o = {values: ['2017-06-17']}
dayjs(o).isValid() === false

Expected behavior
I hope that when an Object has property of values , you should cover it.

like this

const o = {values: ['2017-06-17']}
dayjs(o).isValid === dayjs(o.values).isValid() === true

Information

  • Day.js Version [e.g. v1.10.2]
  • OS: window
  • Browser : chrome 91.0.4472.106
  • Time zone: [e.g. GMT-07:00 DST (Pacific Daylight Time)]
@imwh0im
Copy link
Contributor

imwh0im commented Jun 21, 2021

My guess, is that this is due to the different handling for invalid values.
look that here. 👇

const o = {values: ['2017-06-17']};

moment(o); // Moment<2021-06-21T00:00:00+09:00>
moment(o).format('YYYY-MM-DD'); // 2021-06-21

dayjs(o); // M {
// '$L': 'en',
// '$d': Invalid Date,
// '$x': {},
// '$y': NaN,
// '$M': NaN,
// '$D': NaN,
// '$W': NaN,
// '$H': NaN,
// '$m': NaN,
// '$s': NaN,
// '$ms': NaN
// }
dayjs(o).format('YYYY-MM-DD'); // Invalid Date

Please give feedback if my comment it's wrong.

@admin00001
Copy link
Author

I am not sure that you aleady understand my point; what i say is dayjs(o).isValid() , not dayjs(o).format('YYYY-MM-DD') ;

@imwh0im
Copy link
Contributor

imwh0im commented Jun 21, 2021

used format() it was for understand.

An important point is moment(o) and dayjs(o) values.

moment(o) it was return "now". (my timezone is Korea)
but dayjs(o) return "Invalid Date".

"moment" and "dayjs" is returned unintentional values.

i guess it was means parameter o is not suppported.
so commented to you.

You may not understand the explanation.. because of My English is not good enough. 😢
but i Hope this helps

@BePo65
Copy link
Contributor

BePo65 commented May 29, 2022

Looking at the documentation of moment object parsing you see that moment requires an object with properties like 'year' or 'month' to parse it to a date.

What @imwh0im was trying to explain is that the result from moment is valid, because in the case of an object with invalid properties moment simply takes the current date and that of course is valid (not sure, if this is what you had in mind when checking for dayjs(o).isValid() === true)

In dayjs you have to use the objectSupport plugin; using this, you get the same result as with moment.

var objectSupport = require("dayjs/plugin/objectSupport");
dayjs.extend(objectSupport);

const o = { year: 2017, month: 6, day: 17 }
resultDayjs = dayjs(o)
resultDayjs.isValid() === true
resultDayjs.format('DD-MM-YYYY')  // '17-07-2017'

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

3 participants