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

前端 未结 12 897
有刺的猬
有刺的猬 2020-11-28 10:37

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

相关标签:
12条回答
  • 2020-11-28 10:43

    You can try this

    $('#startdate').val()
    

    or

    $('#startdate').data('date')
    
    0 讨论(0)
  • 2020-11-28 10:43

    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() 
    
    0 讨论(0)
  • 2020-11-28 10:43

    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

    0 讨论(0)
  • 2020-11-28 10:46

    Try this using HTML like here:

    var myDate = window.document.getElementById("startdate").value;
    
    0 讨论(0)
  • 2020-11-28 10:51

    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.

    0 讨论(0)
  • 2020-11-28 10:51
    $("#startdate").data().datepicker.getFormattedDate('yyyy-mm-dd');
    
    0 讨论(0)
提交回复
热议问题