Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery datepicker set defaultvalue

I use this date picker. I need input to be empty on page loading, but set default value to today on datepicke popap. Now when I jast write:

$(".dp").datepicker({
                format: 'dd.mm.yyyy',
                startDate: '01.01.2012',
                endDate: ''
});;

It's first Janary 1970 by default when datapicker is popuping. How can I change default value?

like image 897
xander27 Avatar asked Dec 16 '22 00:12

xander27


2 Answers

quick but works

  var myDate = new Date();
  var date = myDate.getFullYear() + '-' + ('0'+ myDate.getMonth()+1).slice(-2) + '-' + ('0'+ myDate.getDate()).slice(-2);
  $("#datepicker").val(date);
like image 123
Sebastian Castaldi Avatar answered Jan 02 '23 17:01

Sebastian Castaldi


You can do it from the show event, but I have to say that datepicker's documentation is really lacking. Here's how:

(function() {

  $(".dp").datepicker({
    format: 'dd.mm.yyyy',
    startDate: '01.01.2012',
    endDate: ''
  }).on("show", function() {
    $(this).val("01.05.2012").datepicker('update');
  });

})();

Naturally, where I have 01.05.2012, put the actual date.

like image 44
T.J. Crowder Avatar answered Jan 02 '23 17:01

T.J. Crowder