Bootstrap datepicker — how to get date as a string in the right format?

前端 未结 3 1614
太阳男子
太阳男子 2021-02-19 08:15

I\'m using Bootstrap datepicker. The answer is probably obvious, but I can\'t figure out how to get the selected date as a string in the specified format. I used:



        
相关标签:
3条回答
  • 2021-02-19 08:27

    If you want the value as a string use: var value = $("#myDate input").val();

    The method getDate returns an object

    0 讨论(0)
  • 2021-02-19 08:32

    The data-date-format="mm/dd/yyyy" needs to be set on the input:

    <input type="text" data-date-format="MM/DD/YYYY">
    

    Your jQuery should also be changed to:

    var value = $("#myDate input").datepicker("getDate");
    

    Or you can just keep things simple with no wrapping div and with this your original jQuery would work fine and you wouldn't have to specify input:

    <input type="text" id="myDate" data-date-format="YYYY-MM-DD">
    

    Suggestion
    I would recommend creating a class to format the datepicker. So that you can then add the .datepicker class to any input field and it would work without having to specify another variable.

    Fiddle
    Here is working code with alert of date http://jsfiddle.net/doitlikejustin/HFuDg/1/

    0 讨论(0)
  • 2021-02-19 08:41
    var value = $("#myDate").datepicker("getFormattedDate");
    
    0 讨论(0)
提交回复
热议问题