Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the selected date value while using Bootstrap Datepicker?

Using jquery and the Bootstrap Datepicker, how do I get the new date value I selected using the Bootstrap Datepicker? FYI, I'm using Rails 3 and Coffescript.

I set up the datapicker using:

<input id="startdate" class="span2 datepicker" type="text" value="" name="startdate" default="2013-05-21" data-date="2013-05-21" data-behavior="datepicker"> <%= submit_tag 'Get Data using date', :id => 'get_data' %>  $(".datepicker").datepicker       endDate: new Date       format: "yyyy-mm-dd"       autoclose: true       minViewMode: 1       todayBtn: "linked" 

When a user clicks on the "get data using date" button next to it, I will use jQuery to get the new date value set by the datepicker, prevent the form from submitting and run an ajax request using that date value. All the ajax is running well, except for getting the correct new date value. I tried both of the following and it doesn't give me the new date, it only return the defaults I initially set.

sd1 = $('#startdate').attr('value') console.log sd1  sd2 = $('#startdate').attr('data-date')) console.log sd2 

I am feeling really stupid right now, but I can't find out how to get the new date value set by the bootstrap datepicker.

like image 955
Ryan Avatar asked May 22 '13 01:05

Ryan


People also ask

How do I get datepicker value?

If you want to get the date when the user selects it, you can do this: $("#datepicker"). datepicker({ onSelect: function() { var dateObject = $(this). datepicker('getDate'); } });

How can we get the current date using datepicker?

To set current date in control to which jQuery UI datepicker bind, use setDate() method. Pass date object which needs to be set as an argument to setDate() method. If you want to set it to current date then you can pass 'today' as argument.

Is there a date picker in bootstrap?

As with bootstrap's own plugins, datepicker provides a data-api that can be used to instantiate datepickers without the need for custom javascript. For most datepickers, simply set data-provide="datepicker" on the element you want to initialize, and it will be intialized lazily, in true bootstrap fashion.


2 Answers

For bootstrap datepicker you can use:

$("#inputWithDatePicer").data('datepicker').getFormattedDate('yyyy-mm-dd'); 
like image 112
Matias Avatar answered Sep 28 '22 17:09

Matias


You can try this

$('#startdate').val() 

or

$('#startdate').data('date') 
like image 40
Sushanth -- Avatar answered Sep 28 '22 18:09

Sushanth --