Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bootstrap datepicker invalid date issue when i click date twice?

I'm using bootstrap datepicker and I got invalid date issue in a specific case. If I click today date it works fine but when I click the same date again it gives out invalid date issue.

Same is the case for all the dates as I have seen other plugins where I can't find this issue.

html: <input id="dp1" type="text" class="form-control input-sm" placeholder="Data CheckIn">

javascript:

$("#dp1").datepicker({
    format: "mm-dd-yyyy",
    viewMode: 'days',
    todayHighlight: true 
}).on('changeDate', function (ev) {
    var a = $('#dp1').datepicker('getDate');
    $(this).datepicker('hide');
    alert(a);
});

jsfiddle example

like image 596
Sherry Avatar asked May 05 '15 05:05

Sherry


People also ask

How do I change the default date format in bootstrap Datepicker?

Go to line 1399 and find format: 'mm/dd/yyyy' . Now you can change the date format here.

How do I change Datepicker format from DD MM to YYYY?

The jQuery DatePicker plugin supports multiple Date formats and in order to set the dd/MM/yyyy Date format, the dateFormat property needs to be set. The following HTML Markup consists of a TextBox which has been made Read Only.

How do I know if my Datepicker is loaded?

You can check to see if the datepicker script has loaded using: if ($. fn. datepicker) { // ... }


3 Answers

var currentDate;
$('#dp1').datepicker({
    format: "mm-dd-yyyy",
    viewMode: 'days',
    todayHighlight: true

    // currently picked date
  }).on('show', function() {
    currentDate = $(this).val();
  })
  // if no date picked replace with previous date
  .on('changeDate', function(ev) {


    if ($(this).val() === '' || $(this).val() === null) {
      $(this).val(currentDate).datepicker('update');
    }
    var a = $('#dp1').datepicker('getDate');
    $(this).datepicker('hide');
    alert(a);

  });
like image 145
Namitha Avatar answered Oct 20 '22 21:10

Namitha


After an effort of a day I solved issue by myself by doing a little change in plugin.

In bootstrap-datepicker.js method _toggle_multidate line no 1024 has been commented.

else if (ix !== -1)
{
    //this.dates.remove(ix);
}

and it will work like a charm. I hope this can help.

Please try and do let me know if this case solve your issue Thanks

like image 36
Sherry Avatar answered Oct 20 '22 20:10

Sherry


you can use delegate, it does work fine

$("body").delegate('.date' ,'focusin', function() {
     $(this).datepicker({
         autoclose: true,
         viewMode: 'days',
         todayHighlight: true
     });
});
like image 35
sivan.liao Avatar answered Oct 20 '22 21:10

sivan.liao