uxsolutions / bootstrap-datepicker

A datepicker for twitter bootstrap (@twbs)
Apache License 2.0
12.66k stars 6.06k forks source link

Change Date #1726

Open SpringsTea opened 8 years ago

SpringsTea commented 8 years ago

I'm going to have an aneurysm. I know this stuff is all over here, and there's tons of answers to this question, but non of them WORK.

I'm just trying to change the chosen date of a picker. Thats all. How easy.

Heres my input: <input type="text" class="input-small form-control" name="start" id="date-range-start" />

I have a picker that does some stuff:

$('#date-range-start').datepicker({
  defaultDate:'now',
    startDate: startDate,
    endDate: ToEndDate,
    autoclose: true
})
  .on('changeDate', function(selected){
      startDate = new Date(selected.date.valueOf()+ 1000*3600*24);
      startDate.setDate(startDate.getDate(new Date(selected.date.valueOf())));
      $('#date-range-end').datepicker('setStartDate', startDate);
      filterTable();
  });

And basically I want to be able to programatically set the selected date when I do something somewhere else. Theres a billion different ways people give to set the date including:

$('#date-range-start').datepicker("update", new Date());
$('#date-range-start').datepicker('update', '2015-01-01');
$('#date-range-start').datepicker('setDate',new Date());
$('#date-range-start').datepicker("update", new Date());
$('#date-range-start').data({date: '2015-01-01'});

and many others that I've tried and forgotten. Sometimes people say 'aw you gotta refresh the picker with $('#date-range-start').datepicker("update"); and some people don't seem to need it, but nothing works for me. Theres no error, they all return the input item, they all remain blank.

Help me I'm dying.

SpringsTea commented 8 years ago

So over a month later I still have no solution to this. I see now the issue stems from the 'startDate' option. Removing this option fixes the issue. Outside of this issue startDate works as intended when using the picker, theres no error when using it. If I don't set startDate on setup and then add it programatically with 'setStartDate' I won't be able to set the date again.

Theres no way this isnt a bug.

YGKtech commented 7 years ago

Same issue here, none of the documented update methods actually work. At all.

There are 466 open issues on this repo with minimal response from the developers; I understand that this is open source software and nobody owes me anything, but it's really irresponsible to leave a project in this state. If these bugs aren't going to be fixed, then you should just go ahead and mark the repo inactive.

YGKtech commented 7 years ago

I was eventually able to get it working using this bit of code:

var old_date = $("#date_picker").datepicker('getDate'); var datestring = old_date.getMonth()+1 + "/" + (old_date.getDate()+modifier) + "/" + old_date.getFullYear(); $("#date_picker").datepicker('setDate', datestring);

The documentation needs to be updated to correctly indicate the expected format of the input data, none of the documented code for this will actually work.

Azaret commented 7 years ago

Hi @SpringsTea, I know it's late for your issue but if by any means you still needs help on your issue, you can reply here and I'll get on your case.

@YGKtech : The repo is on slow mode since #1167, we doing our best to not let this repo die but we all have lives and can't work full time on this project. update works as expected as far as I know. The format expected for any methods is the one set in the format option.

YGKtech commented 7 years ago

@Azaret : then that needs to be in the documentation, because the current docs just show the update method consuming a raw date object, which is way off the mark.

Azaret commented 7 years ago

The current documentation shows examples with both Date object and formatted strings. But I concede that it is not clearly stated in the documentation that the format option set the internal format for dates and therefor impact directly all methods. We will fix that.

YGKtech commented 7 years ago

@Azaret : Thank you, I think some work on the documentation will clear up a lot of the complaints people have about this plugin.

ptijul commented 6 years ago

I was also not able to set the date by javascript without some issues (tested with latest version 1.7.1). Once we set the date, the control is back to its default settings :

Here is the code:

var datePicker = $('#dtpCompo');

datePicker.datepicker({
     format: 'dd.mm.yyyy'
});
datePicker.datepicker('setDate', dateComposition);
qs-labs commented 5 years ago

I was able to work around this limitation by setting the time of my date objects to match the internal date objects used by the datepicker. Without setting the time, the datepicker may interpret the time component of a JS Date object as exceeding one's start, endDate window and therefore will reject one's attempts at setting the date. Code as follows:

var today = new Date();
today.setHours(0, 0, 0, 0);

$("#datepicker").datepicker('setDate', today);