Bootstrap 将当前日期设置为在引导数据选取器中选定的日期

2o7dmzc5  于 2023-02-14  发布在  Bootstrap
关注(0)|答案(6)|浏览(158)

我在代码中使用了bootstrap-datepicker,如何在JavaScript中选择当前日期并显示为选中状态?
经过研究,这是我正在使用的代码,但当前日期未显示为选定:

var today = moment();

datepicker1.datepicker({
    endDate: today.toDate()
}); 

datepicker2.datepicker({
    startDate: today .toDate(),
    endDate: today.toDate()
});

datepicker1.datepicker('setDate', today.toDate());
datepicker2.datepicker('setDate', today.toDate());

datepicker1.datepicker('update');  //update the bootstrap datepicker
datepicker2.datepicker('update');
fdbelqdn

fdbelqdn1#

我正在使用 Bootstrap datepicker.下面的代码为我工作:

$('#my-datepicker').datepicker('setDate', 'now');
ve7v8dk2

ve7v8dk22#

不了解您正在使用的代码,没有HTML标记也没有多大意义,
下面是一个工作示例,说明您所追求的目标

$(document).ready(function() {
  var date = new Date();
  var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  var end = new Date(date.getFullYear(), date.getMonth(), date.getDate());

  $('#datepicker1').datepicker({
format: "mm/dd/yyyy",
todayHighlight: true,
startDate: today,
endDate: end,
autoclose: true
  });
  $('#datepicker2').datepicker({
format: "mm/dd/yyyy",
todayHighlight: true,
startDate: today,
endDate: end,
autoclose: true
  });

  $('#datepicker1,#datepicker2').datepicker('setDate', today);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/js/bootstrap-datepicker.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/css/bootstrap-datepicker.min.css" />
<div class="row">
  <div class="col-lg-2">
<div class="panel">
  <fieldset>
    <div class="form-group">
      <label for="datepicker1">Date</label>
      <input type="text" class="form-control" id="datepicker1">
    </div>
  </fieldset>
  <fieldset>
    <div class="form-group">
      <label for="datepicker2">Date2</label>
      <input type="text" class="form-control" id="datepicker2">
    </div>
  </fieldset>
</div>
  </div>
</div>
swvgeqrz

swvgeqrz3#

使用minDate : moment()时选择当前日期的问题。
解决方案是使用以下代码来分配datetimepicker:

$('#datetimepicker6').datetimepicker({ 
    format: 'DD/MM/YYYY', 
    minDate: moment().millisecond(0).second(0).minute(0).hour(0),
    clear : false 
});
mznpcxlj

mznpcxlj4#

这对我有用

$("#order_date").datepicker( {
   autoclose: true,
});

var orderDate = '2021-03-05';
$('#order_date').datepicker('update', new Date(Date.parse(orderDate)));

Reference

ltqd579y

ltqd579y5#

$(document).ready(function() {
  var date = new Date();
  var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  var end = new Date(date.getFullYear(), date.getMonth(), date.getDate());

  $('#datepicker1').datepicker({
format: "mm/dd/yyyy",
todayHighlight: true,
startDate: today,
endDate: end,
autoclose: true
  });
  $('#datepicker2').datepicker({
format: "mm/dd/yyyy",
todayHighlight: true,
startDate: today,
endDate: end,
autoclose: true
  });

  $('#datepicker1,#datepicker2').datepicker('setDate', today);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/js/bootstrap-datepicker.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/css/bootstrap-datepicker.min.css" />
<div class="row">
  <div class="col-lg-2">
<div class="panel">
  <fieldset>
    <div class="form-group">
      <label for="datepicker1">Date</label>
      <input type="text" class="form-control" id="datepicker1">
    </div>
  </fieldset>
  <fieldset>
    <div class="form-group">
      <label for="datepicker2">Date2</label>
      <input type="text" class="form-control" id="datepicker2">
    </div>
  </fieldset>
</div>
  </div>
</div>
mefy6pfw

mefy6pfw6#

有了这个,你可以突出显示当前日期!

$('#datetimepicker6').datetimepicker({ 
        todayHighlight:true
    })

相关问题