Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically set bootstrap-datepicker's date value?

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

like image 982
corinacallan Avatar asked Jul 16 '12 15:07

corinacallan


People also ask

How do I set datepicker value?

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.

How do I change the default date in datepicker?

Syntax: $(". selector"). datepicker( {defaultDate:"+6"} );

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

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);


2 Answers

This works for me

$(".datepicker").datepicker("update", new Date()); 
like image 88
Imran Rashid Avatar answered Sep 19 '22 17:09

Imran Rashid


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'); 
  1. set the calendar date on property data-date
  2. update (required)
  3. set the input value (remember the input is child of datepicker).

this way you are setting the date to '2012-08-08'

like image 40
MG_ Avatar answered Sep 17 '22 17:09

MG_