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 defaults to mm/dd/yyyy unless stated in all constructors because it's not possible to set up default options #140

Open
szotsaki opened this issue Apr 16, 2023 · 1 comment

Comments

@szotsaki
Copy link

In Bootstrap datepicker you could set up defaults before initialising an instance in the following way:

$.fn.datepicker.defaults.todayHighlight = true;
$.fn.datepicker.defaults.language = "en-GB";
[]

This option is missing from the Vanillajs version, unfortunately. If you don't want to one by one specify the format (like the non-US ISO format) in all of your Datepicker constructors, there's no other option.

  • It's not possible to use setOptions() because it's called after construction is done, and the date will be parsed in a bad way initially. setOptions() will reformat that bad date to the nicer ISO date.
  • Overriding the en locale doesn't work
@mymth
Copy link
Owner

mymth commented Apr 17, 2023

I think it’s not really a bootstrap-datepicker's "feature" and you hacked because it was visible and modifiable due to JavaScript language's specs.

Anyway, how about doing like this?

class MyDatepicker extends Datepicker {
  constructor(element, options = {}, rangepicker = undefined) {
    super(element, Object.assign({
      todayHighlight: true,
      language: 'en-GB',
    }, options), rangepicker);
  }
}

// ...

const elem = document.querySelector('input.date');
const datepicker = new MyDatepicker(elem);

https://codepen.io/mymth/pen/WNaxrJV

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

2 participants