NRKirby NRKirby - 3 months ago 6
Javascript Question

Unable to change format of jQuery datepicker

I am trying to set the format of a jQuery datepicker to

dd-mm-yy
, and log it to the console when I click a button. The code I have is:

$('#ajaxButton').click(function () {
var date = $('#datepicker').datepicker({
dateFormat: "dd-mm-yy"
}).val();
console.log(date);
});



The date
09/01/2016
is being logged to the console


I can't figure out why, can anyone help?

Answer

Below code will give you date in your given format :

$('#ajaxButton').click(function () {
  console.log(date.val());
});
var date = $('#datepicker').datepicker({
  dateFormat: "dd-mm-yy"
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<input type="text" id="datepicker">
<input type="button" id="ajaxButton" value="click"/>

Comments