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

format data-date #135

Open
avalenti38 opened this issue Feb 2, 2023 · 3 comments
Open

format data-date #135

avalenti38 opened this issue Feb 2, 2023 · 3 comments

Comments

@avalenti38
Copy link

Hi et thank you for this work.

How i can select the data-date correctly ? i have to parse it with new date or it is possible to change it with new format YYYY-MM-DD in the parameter ?

@mymth
Copy link
Owner

mymth commented Feb 13, 2023

Sorry if I don't understand correctly.

Date picker parses given date strings using the format specified with the format option. So, if the format of the date given by the server (or whatever) is different from the one you use for the UI, you need to parse it first, then set the resulting Date object programmatically with setDate().

Or, you may be able to create a custom parser that supports both ISO and local date formats, like this:

var datepicker = new Datepicker(elem, {
  format: {
    toValue: (date) => {
      const format = typeof date === 'string' && date.match(/^\d{4}(-\d\d){2}$/)
        ? 'yyyy-mm-dd'
        : 'dd/mm/yyyy';
      return Datepicker.parseDate(date, format, 'fr');
    },
    toDisplay: date => Datepicker.formatDate(date, 'dd/mm/yyyy', 'fr'),
  }
};

@JoshuaAlzate
Copy link

I can't do this on typescript given the toValue is only accepting string

@mymth
Copy link
Owner

mymth commented Mar 12, 2023

@JoshuaAlzate, can you elaborate on what you are trying to do?
format.toValue() is called when a date passed to setDate() is neither a Date object nor a number, and the types setDate() accepts are string, Date, and number. So, there should be no problem with toValue()'s only accepting string.

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