Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap datepicker format date differently on display than on value

I want to use Twitter Bootstrap's datepicker. I want the actual input to DISPLAY in the format mm/dd/yyyy but the value of the object I want it to create/pass should be in yyyy-mm-dd. I am aware of this property:

"data-date-format" => "mm-dd-yyyy"

But that changes both the way the date is displayed and how the value is formatted. I also have this in my JS:

$(this).datepicker({
  format: 'yyyy-mm-dd',
  autoclose: true,
  todayHighlight: true,
  pickTime: false
});

I'm not really sure what the format part is doing, but changing it doesn't change the value that is created by the input.

like image 293
Captain Stack Avatar asked Mar 12 '15 22:03

Captain Stack


People also ask

How to add datepicker in Bootstrap bootstrap?

Instead of entering the date manually in the input field, we can pick the date from the Datepicker dialog. The datepicker plugin of bootstrap uses jQuery, which helps to add Datepciker in the form of an input element.

How to set the date format in datepicker?

In our program, if we require datepicker with our date format like dd/mm/yyyy, yyyy-mm-dd, dd/mm/yyyy, dd-mm-yyyy etc, it will be very easy for us to do this. In order to set the date format, we only need to add format of one argument and then we will add our required format, which is shown in the following example:

What happens if the datepicker is set to false?

If false, the datepicker will be prevented from showing when the input field associated with it receives focus. Date or String. Default: Beginning of time The earliest date that may be selected; all earlier dates will be disabled. Date should be in local timezone.

How do I Close a datepicker in HTML?

If true, displays a “Clear” button at the bottom of the datepicker to clear the input value. If “autoclose” is also set to true, this button will also close the datepicker. String. Default: “body” Appends the date picker popup to a specific element; eg: container: ‘#picker-container’ (will default to “body”) String, Array.


2 Answers

When I am faced with such a problem, where I want data displayed differently than I want it communicated, I usually write a short script to copy the value into a hidden element where I maintain the 'correctly'-formatted data which the user does not see.

This is the best solution I can offer at the moment, as I do not know of any way to convince the Bootstrap Datepicker to use two formats simultaneously.

like image 160
Jared Clemence Avatar answered Sep 28 '22 10:09

Jared Clemence


there's a solution from bootstrap for this problem.

as said on the docs

you can set format as an object with 2 parsing functions: toValue and toDisplay.

$('.datepicker').datepicker({
format: {
    /*
     * Say our UI should display a week ahead,
     * but textbox should store the actual date.
     * This is useful if we need UI to select local dates,
     * but store in UTC
     */
    toDisplay: function (date, format, language) {
        var d = new Date(date);
        d.setDate(d.getDate() - 7);
        return d.toISOString();
    },
    toValue: function (date, format, language) {
        var d = new Date(date);
        d.setDate(d.getDate() + 7);
        return new Date(d);
    }
  },
   autoclose: true 
});
like image 32
Sahar Zehavi Avatar answered Sep 28 '22 08:09

Sahar Zehavi