使用 Bootstsrap Jquery 在日期选择器中将下一个月和上一个月替换为下一天和前一天
Replace Next and Previous Month by Next And previous Day in datepicker using Bootstsrap Jquery
我有一个 jquery bootstrap 内联日期选择器,我想递增和递减当天
我试试这个:
// Html Code
<div id="datepicker"></div>
// JS Code
$('#datepicker').datepicker({
todayHighlight: true,
language: 'en',
});
$('.btn-next').on("click", function () {
var currentDate = $('#datepicker').datepicker('getDate');
var selectedDate= new Date(currentDate);
// Increase current day by +1
selectedDate.setDate(selectedDate.getDate() + 1);
$('#datepicker').datepicker('update',selectedDate);
});
$('.btn-prev').on("click", function () {
var currentDate = $('#datepicker').datepicker('getDate');
var selectedDate= new Date(currentDate);
// Decrease current day by -1
selectedDate.setDate(selectedDate.getDate() - 1);
$('#datepicker').datepicker('update',selectedDate);
});
但它不再起作用了,对这个运算符有什么想法
您不需要再次转换从日期选择器 +1
或 -1
获得的日期并在日期选择器中添加新值。
演示代码:
$('#datepicker').datepicker({
todayHighlight: true,
language: 'en',
});
$('.btn-next').on("click", function() {
var currentDate = $('#datepicker').datepicker('getDate');
currentDate.setDate(currentDate.getDate() + 1);
$('#datepicker').datepicker('update', currentDate);
});
$('.btn-prev').on("click", function() {
var currentDate = $('#datepicker').datepicker('getDate');
currentDate.setDate(currentDate.getDate() - 1);
$('#datepicker').datepicker('update', currentDate);
});
<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.9.0/js/bootstrap-datepicker.min.js" integrity="sha256-bqVeqGdJ7h/lYPq6xrPv/YGzMEb6dNxlfiTUHSgRCp8=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css" integrity="sha256-siyOpF/pBWUPgIcQi17TLBkjvNgNQArcmwJB8YvkAgg=" crossorigin="anonymous" />
<input type="text" id="datepicker">
<button class="btn-prev">Prev</button>
<button class="btn-next">Next</button>
我有一个 jquery bootstrap 内联日期选择器,我想递增和递减当天 我试试这个:
// Html Code
<div id="datepicker"></div>
// JS Code
$('#datepicker').datepicker({
todayHighlight: true,
language: 'en',
});
$('.btn-next').on("click", function () {
var currentDate = $('#datepicker').datepicker('getDate');
var selectedDate= new Date(currentDate);
// Increase current day by +1
selectedDate.setDate(selectedDate.getDate() + 1);
$('#datepicker').datepicker('update',selectedDate);
});
$('.btn-prev').on("click", function () {
var currentDate = $('#datepicker').datepicker('getDate');
var selectedDate= new Date(currentDate);
// Decrease current day by -1
selectedDate.setDate(selectedDate.getDate() - 1);
$('#datepicker').datepicker('update',selectedDate);
});
但它不再起作用了,对这个运算符有什么想法
您不需要再次转换从日期选择器 +1
或 -1
获得的日期并在日期选择器中添加新值。
演示代码:
$('#datepicker').datepicker({
todayHighlight: true,
language: 'en',
});
$('.btn-next').on("click", function() {
var currentDate = $('#datepicker').datepicker('getDate');
currentDate.setDate(currentDate.getDate() + 1);
$('#datepicker').datepicker('update', currentDate);
});
$('.btn-prev').on("click", function() {
var currentDate = $('#datepicker').datepicker('getDate');
currentDate.setDate(currentDate.getDate() - 1);
$('#datepicker').datepicker('update', currentDate);
});
<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.9.0/js/bootstrap-datepicker.min.js" integrity="sha256-bqVeqGdJ7h/lYPq6xrPv/YGzMEb6dNxlfiTUHSgRCp8=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css" integrity="sha256-siyOpF/pBWUPgIcQi17TLBkjvNgNQArcmwJB8YvkAgg=" crossorigin="anonymous" />
<input type="text" id="datepicker">
<button class="btn-prev">Prev</button>
<button class="btn-next">Next</button>