JQuery datepicker set defaultvalue

2020-04-11 07:10发布

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?

4条回答
小情绪 Triste *
2楼-- · 2020-04-11 07:25

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);
查看更多
\"骚年 ilove
3楼-- · 2020-04-11 07:35

In bootstrap-datepicker.js (~ Line 331) change:

date = new Date(1971, 1, 1, 0, 0, 0),

to

date = new Date(),
查看更多
劫难
4楼-- · 2020-04-11 07:37

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.

查看更多
霸刀☆藐视天下
5楼-- · 2020-04-11 07:44

If anyone runs into this, where they want the default value in the input value to show as well as initialize datetimepicker, this works for me. I set the value in the html to data-val and check for that in the ready function. Then, I use moment to covert the string and set the initial picker value:

$(function(){
  if($('#complianceBy').data('val').length){
  $('#complianceBy').data('DateTimePicker').date(moment($('#complianceBy').data('val')));
 }
});
查看更多
登录 后发表回答