Skip to content Skip to sidebar Skip to footer

Bootstrap Datepicker Multiple Formats

I am using Backbone.js with a Bootstrap Datepicker in my form. The brief was to allow the client to use the 'dd.mm.yyyy' format, so I set the option on the datepicker. self.$el.fin

Solution 1:

You can pass functions to datepicker's format option. For really flexible parsing, I used moment.js.

$(".datepicker").datepicker({
  format: {
    toDisplay: function(date, format, language) {
      return moment(date).format("MM/DD/YYYY");
    },
    toValue: function(date, format, language) {
      return moment(date, ["DD.MM.YYYY", "DDMMYYYY"]).toDate();
    }
  }
});

From the bootstrap-datepicker docs:

toDisplay: function (date, format, language) to convert date object to string, that will be stored in input field

toValue: function (date, format, language) to convert string object to date, that will be used in date selection

You may have to play around with the moment() options, but you should be able to get it to where you want it. Check out momentjs's docs as well.


Post a Comment for "Bootstrap Datepicker Multiple Formats"