bootstrap-datepicker icon indicating copy to clipboard operation
bootstrap-datepicker copied to clipboard

Updating date picker causes loss of format

Open LeanneW opened this issue 10 years ago • 12 comments

I am currently using the date picker with the en-GB locale and its working perfectly until I have to call either update or setDate. The date sets fine however the date picker suddenly reverts from dd/mm/yyyy format to mm/dd/yyyy format. It's as if calling either method is reverting the date picker back to default settings.

If you require further information please let me know. I'm using v1.5.1.

LeanneW avatar Jan 27 '16 16:01 LeanneW

Same for me: not only it reverts to default format, but it changes locale to default too.

mynamespace avatar Feb 25 '16 09:02 mynamespace

Is this ever going to be fixed or should we wait for v2?

andreasabilio avatar Nov 11 '16 10:11 andreasabilio

Hi here, can you guys provide some snippets or jsfiddle of your implementation ? I cannot reproduce your issue either in 1.5.1, 1.6.4 or 1.7.0.

Azaret avatar Nov 11 '16 11:11 Azaret

Hi there, I came across this post as I was having similar issues. I realised I had version 1.5.0. I updated and still the issue remains. I am simply calling closedDateDP.datepicker('update', d); I have also tried closedDateDP.datepicker('update', d.toLocaleDateString('en-GB')); and closedDateDP.val(d.toLocaleDateString('en-GB')); The latter of which looks correct, but then results in a null date in the controller. If I select from the calendar, the controller has a value.

Also, having called closedDateDP.datepicker('update',....... I get an additional calendar when I click the calendar icon in the UI! Please can you assist me.

DawnF avatar Nov 18 '16 14:11 DawnF

For further info (in case anyone else finds this thread!) This seems to work for me closedDateDP.datepicker({'update': d.toLocaleDateString('en-GB')}, {culture: 'en-GB'} );

I just set the culture again on update.

DawnF avatar Nov 18 '16 14:11 DawnF

Ironically, the above has now stopped working - It worked I swear, but when I added an update to null the whole lot dies and even removing that the first statement no longer works! Please help me to set the datepicker

DawnF avatar Nov 18 '16 15:11 DawnF

Hi, Once again I need a full sample of what you are doing because as far as I can tell everything is working as expected for me. I'm thinking that there is some misunderstanding about formats here but I can't tell 100% without more details.

I ran those snippets without noticeable issues:

        $('#datepicker0').datepicker();
        $('#button0').on('click', function () {
            $('#datepicker0').datepicker('update', new Date(2016, 10, 18));
        });
        // datepicker0 shows 11/18/2016 as expected
        $('#datepicker1').datepicker({
            language: 'en-GB'
        });
        $('#button1').on('click', function () {
            $('#datepicker1').datepicker('update', new Date(2016, 10, 18));
        });
        // datepicker1 shows 18/11/2016 as expected
        $('#datepicker2').datepicker({
            language: 'en-GB',
            format: 'yyyy-m-d'
        });
        $('#button2').on('click', function () {
            $('#datepicker2').datepicker('update', new Date(2016, 10, 18));
        });
        // datepicker2 shows 2016-11-18 as expected

Please note that all methods are dependent of the format option. Which means that if you set format to yyyy-m-d, then sending 18/11/2016 or 11/18/2016 (or d.toLocaleDateString('en-GB')) won't works, it will need to be 2016-11-18.

Azaret avatar Nov 18 '16 17:11 Azaret

It worked for me by setting the global default format ($.fn.datepicker.defaults.format = 'yyyy-mm-dd';) instead of on initialization ($('#pick-a-date').datepicker({format: 'yyyy-mm-dd'});)

asnoba avatar Jan 30 '17 19:01 asnoba

Hi @Azaret, please look at this jsfiddle. After clicking update button, datepicker is reset to default language and format.

verglor avatar Feb 20 '19 22:02 verglor

I'm having the same issue. I initialise using the following, and it works fine

$.fn.datepicker.defaults.format = 'dd/mm/yyyy'; /* Added after readingthis thread in the hope it will help) */
$('#ValidToDate').datepicker({
            format: "dd/mm/yyyy",
        maxViewMode: 2,
    todayBtn: true,
    defaultViewDate: { year: Date.now.year, month: Date.now.month, day: Date.now.day }
});

Then I call an update

$("#ValidToDate").datepicker('update', '14/05/2019');

After which the datepicker displays 02/05/2019 and acts as though the date is a mm/dd/yyyy format - so when I open the datepicker, it sees the date as the 5th of Feb.

Interestingly, the 14th (dd) becomes February (mm) - so it looks like the 14 is overflowing as 14%12=2?

audigex avatar May 14 '19 16:05 audigex

I'm still having the same issue. Did anybody figured it out?

ggchicote avatar Jun 09 '21 13:06 ggchicote

I managed to fix it with this answer

MatheusMiliorini avatar Nov 03 '21 13:11 MatheusMiliorini