chrisdavies / tiny-date-picker

A small, modern, dependency-free date picker
https://chrisdavies.github.io/tiny-date-picker/
415 stars 87 forks source link

Parsing dates from different locales #48

Closed harrygr closed 7 years ago

harrygr commented 7 years ago

The trouble with only keeping of the string value of the date in state is that JS's parsing is difficult from different locales.

E.g. If my locale is en-GB I display dates as '22/06/2017'. There is not an easy way to parse this into a Date object:

new Date('22/06/2017') // InvalidDate
new Date('10/06/2017') // Fri Oct 06 2017 00:00:00 GMT+0100 (BST) <- Incorrect for my locale

It seems that the date-picker should remember its current date as a date object, and its options (e.g. min, max, preselectedDate) should be passed as date objects and not rely on the parse function to translate themselves.

chrisdavies commented 7 years ago

Hm. This is by design. I want to allow the consumers of Tiny Date Picker to be able to parse dates themselves so they can handle localization details as needed. So, if an app is using Tiny Date Picker, and needs to support English, German, and French, it probably needs to pull in a library to parse dates according to the user's locale.

The date picker does remember its current date as a date object, actually, but it also needs to be able to parse the date that the user inputs or that the input box is displaying, which is generally localized, hence the implementation.

I'm considering writing a v3 of this library, though, because it does have a lot of limitations and its architecture isn't great.

If you can think of a good way to handle localization and proper parsing of dates across locales, let me know!

paiva-thiago commented 5 years ago

@chrisdavies, Do you think is a good idea to use a flag to allow developers to customize?