如何初始化引导程序日历中的月份



简单问题。我在这个网站上有一系列的引导日历:

http://hollandandbarnes.azurewebsites.net/entertainment/upcoming-events/

我想知道如何设置日历的第一个月。有办法做到这一点吗?

谢谢。

Ambrish的答案可能适用于最新版本的Bootstrap,但在3.3.7版本中,我发现这是可行的:

const d = new Date();
d.setMonth(desiredMonth); // where desiredMonth is a number between 0 and 11
$('#datepicker').datepicker('setDate', d);

您可以像这样设置年份和月份

$('#datepicker').datepicker({
//..
defaultViewDate: {
year: 2020, //can be omitted.
month: 10-1 //starts at 0, so subtract 1 to reach actual month like 10 -1 for October or use index from 0, 0 = Jan and so on..
}
});

示例:运行代码片段

$(document).ready(function(){
$('#datepicker').datepicker({
//..
defaultViewDate: {
year: 2020,
month: 10-1
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/js/bootstrap-datepicker.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" />
Click the field
<input type="text" id="datepicker" />

最新更新