I am using bootstrap datepicker along with a line highchart.
I want the datepicker date to update when the chart is zoomed. Once that event triggers I update the values of the datepicker. The values update fine but when the calendar is clicked, the date on the popup calendar is still the old date.
Here is my datepicker:
<div class="input-append date pull-right" id="dpStartDate" data-date="@DateTime.Now.AddMonths(-1).ToString("dd/MM/yyyy")" data-date-format="dd/mm/yyyy"> <input class="span2" id="startDateText" size="16" type="text" value="@DateTime.Now.AddMonths(-1).ToString("dd/MM/yyyy")" readonly=""> <span class="add-on"><i class="icon-th"></i></span> </div>
I have tried just updating the values using this code:
$('#dpStartDate').attr('data-date', startDate); $('#startDateText').attr('value', startDate);
which updates the dates fine but doesn't set the calendar.
I've also tried triggering the onChange event, which also updates the dates but not the calendar:
$('#dpStartDate').trigger('changeDate', startDate); $('#dpStartDate').datepicker() .on('show', function (ev) { ev.stopPropagation(); }) .on('changeDate', function (ev, date) { var startDate = new Date(); if (date != undefined) { startDate = date; $('#dpStartDate').attr('data-date', startDate); $('#startDateText').attr('value', startDate); } else { startDate = ev.date; $('#dpStartDate').attr(startDate.getDate() + '/' + (startDate.getMonth() + 1) + '/' + startDate.getFullYear()); $('#startDateText').attr(startDate.getDate() + '/' + (startDate.getMonth() + 1) + '/' + startDate.getFullYear()); } $('#dpStartDate').datepicker('hide'); ev.stopPropagation(); });
Does anyone know if it's even possible to update the calendar like that?
Thanks
If you want to set a value for an input type 'Date', then it has to be formatted as "yyyy-MM-dd" (Note: capital MM for Month, lower case mm for minutes). Otherwise, it will clear the value and leave the datepicker empty.
Syntax: $(". selector"). datepicker( {defaultDate:"+6"} );
var year = 2014; var month = 5; var day = 10; var realDate = new Date(year, month - 1, day); // months are 0-based! $('#MainContent_txtDataConsegna'). datepicker({ dateFormat: 'dd/mm/yyyy' }); // format to show $('#MainContent_txtDataConsegna'). datepicker('setDate', realDate);
This works for me
$(".datepicker").datepicker("update", new Date());
var startDate = new Date(); $('#dpStartDate').data({date: startDate}).datepicker('update').children("input").val(startDate);
another way
$('#dpStartDate').data({date: '2012-08-08'}); $('#dpStartDate').datepicker('update'); $('#dpStartDate').datepicker().children('input').val('2012-08-08');
this way you are setting the date to '2012-08-08'
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