【发布时间】:2018-07-28 09:17:12
【问题描述】:
我遇到了一个问题,我希望在7 days 之间始终拥有difference 和2 dates
假设如果我的日期设置为 01/01/2018 - 07/01/2018 (dd/mm/yy) 然后点击 next 它将日期设置为 08/01/2018 - 15/01/2018(增加 7 天)
类似地在 previous 的 click 上 08/01/2018 - 15/01/2018 应该给 01/01/2018 - 07/01/2018
jsfiddle:http://jsfiddle.net/rU5Nc/2654/
这是我尝试过的:
$(document).ready(function () {
var date = new Date();
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();
$('#datepicker').datepicker({
minDate: new Date(currentYear, currentMonth, currentDate),
dateFormat: 'dd/mm/yy'
});
});
$(document).on("click", '.next-day', function () {
var date = $('#datepicker').datepicker('getDate');
date.setTime(date.getTime() + (1000*60*60*24))
$('#datepicker').datepicker("setDate", date);
});
$(document).on("click", '.prev-day', function () {
var date = $('#datepicker').datepicker('getDate');
date.setTime(date.getTime() - (1000*60*60*24));
$('#datepicker').datepicker("setDate", date);
});
.next-prev{
display: inline-block;
padding: 0px;
top: -45px;
position: relative;
right: -243px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<p>Date:
<input type="text" id="datepicker" value="01/01/2018 - 07/01/2018" />
</p>
<div class="next-prev">
<div class="next-day">
Next date >
</div>
<div class="prev-day">
previous date >
</div>
</div>
编辑 1
我的预期输出:
Always next previous should have 7 days gap, without conflicting, and result should appear like below (maintaining 7 days gap on each click)
<br/>
<hr>
please don't worry on calendar display as i have multiple select in my project, producing same here is not possible, thanks
<p><input type="text" value="01/01/2018 - 07/01/2018"></p>
【问题讨论】:
-
我只想在每次点击
next or previous时显示7 days与input的差异,就像这样jsfiddle.net/3wxr2vbd/2 或请参阅下面的编辑1 -
我不介意使用
moment.js -
副本展示了如何将任意天数添加到 JavaScript 日期。请至少尝试在您的问题的答案中使用代码,看看您是否能弄清楚。
-
@HereticMonkey,我无法在没有冲突的情况下弄清楚,有时它会超过 31 有时会超过 0 (无法保持 7 天的间隔)跨度>
标签: javascript jquery html node.js datepicker