开发者

how to get the value of jQuery datepicker in dateFormat?

I have a jQueryUI datepicker, which was created with dateFormat: 'dd/mm/yy'. If I use a regular HTTP POST or HTTP GET request, the input is passed effectively in dd/mm/yy format, but when I use the datepicker's getDate method in any script, I get something like 'Wed Oct开发者_StackOverflow中文版 05 2011 00:00:00 GMT-0430 (Venezuelan Standard Time)'.

Is there a way I could get the date in the dateFormat of the datepicker (or any format)?

Of course I could use the value of the text input that contains the datepicker, but I would prefer using a method from the widget.


getDate() should be returning a JavaScript Date object, so any of the Date methods should work for you.

I.E.

var date = $("#yourId").datepicker('getDate');
console.log(date.getDay()); //Returns day of the week 0-6
console.log(date.getDate()); //Returns day of the month 1-31
//etc...

Date Object Reference


Try something like this:

var date = $('#datepicker').datepicker({ dateFormat: 'dd/mm/yy' });


To console.log and deal with datepicker function you can try this:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Datepicker - Icon trigger</title>

  <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>
  <script>
  $( function() {
    $( "#datepicker" ).datepicker({

      buttonImageOnly: true,
      buttonText: "Select date"
    });

    $('#datepicker').datepicker()
    .on("input change", function (e) {
    console.log("Date changed: ", e.target.value);
});
  } );


  </script>
</head>
<body>

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


</body>
</html>


This is how I do it :

how to get the value of jQuery datepicker in dateFormat?

jQuery("#select-date-id").datepicker({
    dateFormat: "yy-mm-dd",
    onSelect: function (selected) {
        var selectedDate = $(this).datepicker('getDate');
        console.log(selectedDate); // log the value in the console
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜