简体   繁体   中英

bootstrap datepicker set default date not working

I am using bootstrap datepicker.I want to show 01-01-1960 as selected date in my calendar when it is loaded first time.

javascript

 <script src="~/Content/js/datepicker/bootstrap-datepicker.js"></script>
 <link href="~/Content/js/datepicker/datepicker.css" rel="stylesheet" />

 <script type="text/javascript">
$(document).ready(function () {
      $('.datepicker-input').datepicker({
        defaultViewDate: { year: 1960, month: 01, day: 01 },
        format: 'mm-dd-yyyy',
    });

});

I am using this theme - http://flatfull.com/themes/scale/form-elements.html .

You can set default date like :

 <script src="~/Content/js/datepicker/bootstrap-datepicker.js"></script>
 <link href="~/Content/js/datepicker/datepicker.css" rel="stylesheet" />

 <script type="text/javascript">
$(document).ready(function () {
      $('.datepicker-input').datepicker({
        'setDate', new Date(1960, 01, 01 ),
        'format': 'mm-dd-yyyy',
    });

});
</script>

Try with this.

<script type="text/javascript">    
$(document).ready(function () {

        $('.datepicker-input').datepicker({
        dateFormat: 'yy-mm-dd'
    });
          $('.datepicker-input').datepicker('setDate', new Date(1960,00,01));

          $('.datepicker-input').val('');
    });
</script>

It Works for me.

$('.datepicker-input').datepicker({
    useCurrent: false,
    setDate: new Date(1960, 01, 01),
});

Set useCurrent to false , then setDate would work.

To fix the following error:

error-Uncaught TypeError: data[option] is not a function

You should use the option setValue instead of setDate!

$(".datepicker-input").datepicker("setValue", new Date());

I found the simple solution for setting default value on this problem.

According to the research from google, you cannot simple add 'default date' option here but you can set 'setDate' option. Thus, For example...

<div class="fullDate input-group date" id="atdcRegReqDtDiv">
<input type="text" class="form-control inputstl" id="REQ_DT">
<span class="input-group-addon"><i class="dtPick glyphicon glyphicon-calendar"></i></span>
</div>

Add the setDate option, to the object, and then put empty value to the text input.

$('#atdcRegReqDtDiv').datepicker('setDate', "2018-09-14");
$('#REQ_DT').val("");

Then, you can see the empty input box with the calendar has default Year/Month.

Good Luck.

您可以使用Jquery defaultDate设置默认日期:

defaultDate: new Date(1960, 01, 01);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM