Dayjs: Validating date using a locale

Created on 1 Oct 2018  路  14Comments  路  Source: iamkun/dayjs

Hi!

Is there a way to validated a date using a locale? Something like dayjs('15/10/2018', {locale: 'pt-br'}).isValid()?

Thank you!

Most helpful comment

Yes, the #340 parses using only the specified format. It does not pass the input to the Date constructor as a fallback. For example:

dayjs('15/10/2018', { format: 'DD/MM/YYYY' }).isValid()

You see, that you do not need to provide the locale, but you need to know the Portuguese official date format. If you wanted to specify the format with the help of the locale and not so explicitly, this PR could join forces with #305 to specify a localizable date-only format, for example:

dayjs('15/10/2018', { format: 'L', locale: 'pt-br' }).isValid()

Of course, you would have to know, that "L" means date-only and how it translates in Portuguese :-)

All 14 comments

That exactly code doesn't work?

According to this, it should.

No, it doesn't. I tested yesterday withe the last version available.

Ok, checking the code, it seems that the locale settings are only useful for display. Meaning that you need to work with standard formats.

dayjs('15/10/2018', {locale: 'pt-br'}).isValid(); // false
dayjs('10/15/2018', {locale: 'pt-br'}).isValid(); // true
dayjs('10/15/2018', {locale: 'pt-br'}).format('DD/MM/YYYY'); // 15/10/2018

So we have a problem, because 10/15/2018 is a localized date. Is strange to not accept localized dates, with one exception not mentioned on docs. I thought the lib only accepts ISO dates.

10/15/2018 has a format accepted by native Date class.

new Date('10/15/2018') // Mon Oct 15 2018 00:00:00 GMT+0200 (hora de verano de Europa central)

Even so, MDN warns about using this format, it will work but as local, since is not an standard format.

Good point, but is still risky.

Could we have at least a parameter or config to accept only ISO?

What we would win with this configuration?
Check if a date loaded from outside the client is valid? A date inserted by a user?

Exactly. How can I make sure if the date inserted by an user is real if both formats are valid? 136 can help too.

I didn't saw the #340 PR. You'll have to wait until is merged. I hope in a few weeks

Yes, the #340 parses using only the specified format. It does not pass the input to the Date constructor as a fallback. For example:

dayjs('15/10/2018', { format: 'DD/MM/YYYY' }).isValid()

You see, that you do not need to provide the locale, but you need to know the Portuguese official date format. If you wanted to specify the format with the help of the locale and not so explicitly, this PR could join forces with #305 to specify a localizable date-only format, for example:

dayjs('15/10/2018', { format: 'L', locale: 'pt-br' }).isValid()

Of course, you would have to know, that "L" means date-only and how it translates in Portuguese :-)

@prantlf , please, what the L, LTand LTS mean?

@robsonsobral, they are abstract tokens, which translate to strings of the usual Y, M etc. tokens according to the locale of the dayjs instance. You will find the explanation in the description of LocalizableFormat plugin. They originate from Moment.js's localizable format.

Thank you! I didn't know this standard.

From v1.8.7, you could check like this

import 'dayjs/locale/es'
import customParseFormat from 'dayjs/plugin/customParseFormat'
dayjs.extend(customParseFormat)

dayjs('2018 Enero 15', 'YYYY MMMM DD', 'es').isValid()
Was this page helpful?
0 / 5 - 0 ratings