简体   繁体   English

我如何在 datepicker jquery UI 上保存日期的值

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

As asked before, i want to save the selected date on the datepicker in a value.如前所述,我想将日期选择器上的选定日期保存在一个值中。 So as recommended i did the following.所以按照推荐我做了以下事情。

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

But nothing is appearing when i'm doing the console.log of currenDate, not even a warning or an error.但是当我在执行 currenDate 的 console.log 时什么也没有出现,甚至没有出现警告或错误。

Any suggestions why?任何建议为什么? Thanks谢谢

You logic is not correct to get the date from datePicker - To get the date which is showing in the jQuery ui datepicker currently you can use native function of date-picker getDatedatePicker获取日期的logic正确 - 要获取当前显示在jQuery ui datepicker datePicker的日期,您可以使用 date-picker getDate 的native函数

Once you have date you can format it using formatDate method to whatever you would like to save it as!获得日期后,您可以使用formatDate方法将其format为您想要将其另存为的任何内容! It will be saved in the variable which can accessed for other things if required.它将保存在variable ,如果需要,可以accessedvariable

//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

Live Working Demo:现场工作演示:

 $(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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM