Bootstrap-datepicker inline以短格式点击获取日期

时间:2017-03-28 18:59:10

标签: javascript jquery twitter-bootstrap datepicker bootstrap-datepicker

我是初学者并尝试Bootstrap-Datepicker。我在this Demo中使用它嵌入/内联。

它可以工作,但是我想要在点击日期时将所选日期显示在标签/文本框等中。到目前为止,我点击一个按钮后只得到了日期,但更糟糕的是它是一个巨大的线条 Thu Mar 30.2017 00:00:00 GMT +100 (Mitteleuropäische Zeit)。 我希望它像30.03.201730. Mar.2017一样简单。

我该怎么办?

PS。前2个解决方案有效,但可以提供长线。另外2个回复undefined

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Untitled Document</title>
    <!-- Bootstrap -->
    <!-- Include all compiled plugins (below), or include individual files as needed --> 
    <script src="js/bootstrap.js"></script>
    <link href="css/bootstrap.css" rel="stylesheet">
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
    <script src="js/jquery-1.11.3.min.js"></script>

    <!-- Bootstrap Date-Picker Plugin -->
    <script src="js/bootstrap-datepicker.min.js"></script>
    <link rel="stylesheet" href="css/bootstrap-datepicker3.css"/>
  </head>
  <body>
    <div id="pickyDate"> </div>
    <div>
      <input  type="text" placeholder="Date shown here"  id="showDate"/
    </div>
    <!-- Testing here -->
    <div> 
      <button type="button" class="btn btn-danger" onClick="showTestDate()">Danger Button</button>
      <span class="label label-danger" id="DateTest">Danger Label</span>
      <span class="label label-info" id="lbl2">Info Label</span>
      <span class="label label-info" id="lbl3">Info Label</span>
    </div>

    <script>
      $(document).ready(function(){
        $('#pickyDate').datepicker({
          format: "dd/mm/yyyy",
          todayBtn: "linked",
          language: "de",
          daysOfWeekDisabled: "0,6",
          daysOfWeekHighlighted: "4",
          todayHighlight: true,
        }); 
      })
      /* Need it in Format dd mm yyyy */
      function showTestDate(){
        /** works but output is too long */
        var datelist = $('#pickyDate').datepicker('getDate');
        document.getElementById("DateTest").innerText = datelist;   

        var valueDate = $("#pickyDate").datepicker("getDate");
        document.getElementById("showDate").value = valueDate;

        /** works not 'undefined' */
        var ndate = $("#pickyDate").data("datepicker").date;
        document.getElementById("lbl2").innerText = ndate;

        var myDate=window.document.getElementById("pickyDate").value;
        document.getElementById("lbl3").innerText=myDate;
      }
    </script>
  </body>
</html>

2 个答案:

答案 0 :(得分:3)

您必须使用datepicker('getFormattedDate')代替datepicker('getDate')getDate返回一个JavaScript Date对象,该对象将在问题中以长格式提及。

format选项更改为dd.mm.yyyy以获取带有数字月份的日期,或更改为dd-M-yyyy以获取缩写的月份名称。

当您点击changeDate事件的日期时,您可以附加一个列表器来更新您的输入,标签等。

这是一份工作样本:

&#13;
&#13;
$('#pickyDate').datepicker({
  format: "dd.mm.yyyy",
  todayBtn: "linked",
  language: "de",
  daysOfWeekDisabled: "0,6",
  daysOfWeekHighlighted: "4",
  todayHighlight: true,
}).on('changeDate', showTestDate);

function showTestDate(){
  var value = $('#pickyDate').datepicker('getFormattedDate');
  $("#showDate").val(value);
  $("#DateTest").html(value);
  $("#lbl2").html(value);
  $("#lbl3").html(value);
}
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker3.css" rel="stylesheet"/>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/locales/bootstrap-datepicker.de.min.js"></script>

<div id="pickyDate"></div>
<div>
  <input type="text" placeholder="Date shown here"  id="showDate"/>
</div>
<!-- Testing here -->
<div> 
  <button type="button" class="btn btn-danger" onClick="showTestDate()">Get date</button>
  <span class="label label-danger" id="DateTest">Danger Label</span>
  <span class="label label-info" id="lbl2">Info Label</span>
  <span class="label label-info" id="lbl3">Info Label</span> 
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

更改

  

select()

在你的脚本中

  

format: "dd/mm/yyyy",

这是工作的jsFiddle Date Picker Format

希望它满足您的要求。