如何在日期选择器 jquery UI 上保存日期值

How can i save the value of the date on datepicker jquery UI

如前所述,我想将日期选择器上选定的日期保存在一个值中。所以按照建议我做了以下事情。

$( function() {
    var date_choisie = $("#calendrier").datepicker({ dateFormat: "yy-mm-dd" }).val();
    $( "#calendrier" ).datepicker('setDate', new Date());
    console.log(date_choisie);
} );

但是当我执行 currenDate 的 console.log 时,什么也没有出现,甚至没有警告或错误。

有什么建议吗? 谢谢

logic datePicker 获取日期是正确的 - 要获取当前 jQuery ui datepicker 中显示的日期您可以使用 date-picker getDate

native 函数

一旦你有了日期,你就可以 format 使用 formatDate 方法将它保存为任何你想保存的内容!它将保存在 variable 中,如果需要,可以 accessed 用于其他用途。

//get date in a variable
var date_choisie = $('#calendrier').datepicker('getDate');
var formatDate = $.datepicker.formatDate("yy-mm-dd", date_choisie);
console.log(formatDate); //2020-09-20

现场工作演示:

$(function() {
  $("#calendrier").datepicker({
    dateFormat: "yy-mm-dd",
  })
  //set date as current
  $("#calendrier").datepicker('setDate', new Date());

  //get date in a varoable
  var date_choisie = $('#calendrier').datepicker('getDate');
  var formatDate = $.datepicker.formatDate("yy-mm-dd", date_choisie);
  console.log(formatDate); //2020-09-20
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<p>Date: <input type="text" id="calendrier"></p>