最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to have datepicker set default year? - Stack Overflow

programmeradmin2浏览0评论

I am using bootstrap datepicker and I only show the years. When I load the page, I would like to set a default year, tried the following but the current year (2018) is hightlited instead.

$('#sandbox-container div').datepicker({
  format: "yyyy",
  viewMode: "years", 
  minViewMode: "years",
  updateViewDate: false,
  changeYear: true,
  defaultDate: '2014'
});

Thing is this is an Edit page, so the year is ing from the db, therefore I need to be able to set a default year as per the value I am getting from the db.

I am using bootstrap datepicker and I only show the years. When I load the page, I would like to set a default year, tried the following but the current year (2018) is hightlited instead.

$('#sandbox-container div').datepicker({
  format: "yyyy",
  viewMode: "years", 
  minViewMode: "years",
  updateViewDate: false,
  changeYear: true,
  defaultDate: '2014'
});

Thing is this is an Edit page, so the year is ing from the db, therefore I need to be able to set a default year as per the value I am getting from the db.

Share Improve this question asked Jun 3, 2018 at 2:32 rob.mrob.m 10.6k21 gold badges88 silver badges175 bronze badges 9
  • 1 Have you tried startDate instead of defaultDate? bootstrap-datepicker.readthedocs.io/en/latest/… – mr rogers Commented Jun 3, 2018 at 2:46
  • Or even defaultViewDate bootstrap-datepicker.readthedocs.io/en/latest/… – Get Off My Lawn Commented Jun 3, 2018 at 2:47
  • @mrrogers as per the code above, if I do startDate: '2014' I still get current year, not sure what I'm doing wrong – rob.m Commented Jun 3, 2018 at 2:47
  • @GetOffMyLawn same thing, if I use the code in the question and do defaultViewDate: '2014' I still get current year highlighted – rob.m Commented Jun 3, 2018 at 2:48
  • 1 Then that must be patible with format – Get Off My Lawn Commented Jun 3, 2018 at 2:52
 |  Show 4 more ments

1 Answer 1

Reset to default 3

I had to change updateViewDate: false into updateViewDate: true, so:

$('#sandbox-container div').datepicker({
  format: "yyyy",
  viewMode: "years", 
  minViewMode: "years",
  updateViewDate: true,
  changeYear: true,
  defaultViewDate: {year: '2014'}
});
发布评论

评论列表(0)

  1. 暂无评论