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

JqueryRuby on-RailsJquery UiTwitter BootstrapDatepicker

Jquery Problem Overview


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.

Jquery Solutions


Solution 1 - Jquery

For bootstrap datepicker you can use:

$("#inputWithDatePicer").data('datepicker').getFormattedDate('yyyy-mm-dd');

Solution 2 - Jquery

You can try this

$('#startdate').val()

or

$('#startdate').data('date')

Solution 3 - Jquery

Bootstrap datepicker (the first result from bootstrap datepickcer search) has a method to get the selected date.
https://bootstrap-datepicker.readthedocs.io/en/latest/methods.html#getdate

> getDate: Returns a localized date object representing the internal > date object of the first datepicker in the selection. For multidate > pickers, returns the latest date selected.

$('.datepicker').datepicker("getDate")

or

$('.datepicker').datepicker("getDate").valueOf() 

Solution 4 - Jquery

Generally speaking,

$('#startdate').data('date') 

is best because

$('#startdate').val()

not work if you have a datepicker "inline"

Solution 5 - Jquery

this works with me for inline datepicker (and the other)

$('#startdate').data('datepicker').date

Solution 6 - Jquery

If you wan't the date in full text string format you can do it like this:

$('#your-datepicker').data().datepicker.viewDate

Solution 7 - Jquery

Try this using HTML like here:

var myDate = window.document.getElementById("startdate").value;

Solution 8 - Jquery

If you already have a number of dates already highlighted and want to determine which date was last clicked then you'll need the following:

$('#startdate').data('datepicker').viewDate

viewDate returns a JavaScript date object so you'll need to handle it accordingly.

Solution 9 - Jquery

I was able to find the moment.js object for the selected date with the following:

$('#datepicker').data('DateTimePicker').date()

More info about moment.js and how to format the date using the moment.js object

Solution 10 - Jquery

$("#startdate").data().datepicker.getFormattedDate('yyyy-mm-dd');

Solution 11 - Jquery

I tried with the above answers, but they didn't worked out for me; So as user3475960 referred, I used $('#startdate').html() which gave me the html text so i used it as necessary... May be some one will make use of it..

Solution 12 - Jquery

There are many solutions here but probably the best one that works. Check the version of the script you want to use.

Well at least I can give you my 100% working solution for

> version : 4.17.45 > =========================================================
> bootstrap-datetimejs > https://github.com/Eonasdan/bootstrap-datetimepicker Copyright (c) > 2015 Jonathan Peterson

JavaScript

var startdate = $('#startdate').val(); 

The output looks like: 12.09.2018 03:05

Solution 13 - Jquery

The example here provides an example of use 'getFormattedDate' but it does not say how to pass a format into this function. It is actually easy, it goes as a second parameter like so:

$('#date').datepicker('getFormattedDate', 'yyyy-mm-dd');

alternatively

$('#date').data('datepicker').getFormattedDate('yyyy-mm-dd');

or

$(this).data().datepicker.getFormattedDate('yyyy-mm-dd')

All of these approaches are alternatively undocumented.

But there is a documented way to do it:

here there is a format function, where you can separately configure dates "toDisplay" and "toValue"

$('.bootstrap-datepicker-js').datepicker({
    format: {
        toDisplay: function (date, format, language) {
            var d = new Date(date).toLocaleDateString('en-GB', {
                day: 'numeric',
                month: 'short',
                year: 'numeric'
            }).split(' ').join('-');
            return d;
        },
        toValue: function (date, format, language) {
            var d = new Date(date).toLocaleDateString('en-GB', {
                day: 'numeric',
                month: 'short',
                year: 'numeric'
            }).split(' ').join('-');
            return d;
        }
    },
    minViewMode: 1,
    clearBtn: true,
    autoclose: true,
    enableOnReadonly: true
});

if you declare the datepicker like this you will have your $('#date').datepicker('getFormattedDate') formated for your likings

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRyanView Question on Stackoverflow
Solution 1 - JqueryMatiasView Answer on Stackoverflow
Solution 2 - JquerySushanth --View Answer on Stackoverflow
Solution 3 - JqueryLuca RasconiView Answer on Stackoverflow
Solution 4 - JqueryLodato LView Answer on Stackoverflow
Solution 5 - JqueryShadyView Answer on Stackoverflow
Solution 6 - JqueryPedro MartinsView Answer on Stackoverflow
Solution 7 - Jqueryuser3475960View Answer on Stackoverflow
Solution 8 - JqueryBernView Answer on Stackoverflow
Solution 9 - JquerySMAGView Answer on Stackoverflow
Solution 10 - JqueryAlper EbicogluView Answer on Stackoverflow
Solution 11 - JquerySalindaKrishView Answer on Stackoverflow
Solution 12 - JqueryNoWarView Answer on Stackoverflow
Solution 13 - JqueryYevgeniy AfanasyevView Answer on Stackoverflow