-2

Anyone here how do I display today's date in jquery datepicker? Tried these but it doesn't display today's date in datepicker.

$('#from').datepicker({ 
    defaultDate: new Date(), 
    dateFormat: 'yy-mm-dd', 
    onSelect: function () { 
       table.draw(); 
    }, 
    changeMonth: true, 
    changeYear: true 
});

$('#to').datepicker({ 
    defaultDate: new Date(), 
    dateFormat: 'yy-mm-dd', 
    onSelect: function () { 
       table.draw(); 
    }, 
    changeMonth: true, 
    changeYear: true 
});
2

2 Answers 2

1

You can use setDate like below.

$("#from").datepicker().datepicker("setDate", new Date());

$('#from').datepicker({
  defaultDate: new Date(),
  dateFormat: 'yy-mm-dd',
  onSelect: function() {
    //table.draw();
  },
  changeMonth: true,
  changeYear: true
});
$("#from").datepicker().datepicker("setDate", new Date());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/base/jquery-ui.css" media="screen" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
<input type="text" id="from" />

0

You can set the default date via

$( ".selector" ).datepicker( "setDate", "10/12/2012" );

see: https://api.jqueryui.com/datepicker/#method-setDate

Not the answer you're looking for? Browse other questions tagged or ask your own question.