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.
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.
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:
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.
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.
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.
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
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With