英文:
How to increment date and how to disable previous dates in javascript?
问题
我正在使用HTML5日期选择器,类似于[input type="date"],
我只需要增加当前日期+1。我的意思是显示明天的日期和未来的日期,只有当前日期和之前的日期应该处于禁用状态,只显示未来的日期。
有人可以帮帮我吗?
这是我的尝试的代码。
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// 禁用日期
var input = document.getElementById("preDate");
var today = new Date();
var day = today.getDate()+1;
var mon = new String(today.getMonth()+1); // 一月是0!
var yr = today.getFullYear();
if (mon.length < 2) {
mon = "0" + mon;
}
var date = new String(yr + '-' + mon + '-' + day);
input.disabled = false;
input.setAttribute('min', date);
<!-- language: lang-html -->
<div class="form-group col-3 rfdate">
<label class="col-sm-6 control-label p-sm-0">退款日期</label>
<input type="date" class="form-control" id="preDate" name="preDate" />
</div>
<!-- end snippet -->
Fiddle: 代码在这里..
英文:
Im using html5 datepicker like [input type="date"],
I just need to increment current date + 1. i mean show the tomorrow date and future dates only, the current date and previous dates wants to go disable state only show the future dates..
anyone please help me..
here is my tried code.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// Disable date
var input = document.getElementById("preDate");
var today = new Date();
var day = today.getDate()+1;
var mon = new String(today.getMonth()+1); //January is 0!
var yr = today.getFullYear();
if (mon.length < 0) {
mon = "0" + mon;
}
var date = new String(yr + '-' + mon + '-' + day);
input.disabled = false;
input.setAttribute('min', date);
<!-- language: lang-html -->
<div class="form-group col-3 rfdate">
<label class="col-sm-6 control-label p-sm-0">Refund Date</label>
<input type="date" class="form-control" id="preDate" name="preDate" />
</div>
<!-- end snippet -->
Fiddle: Code Here..
答案1
得分: 1
尝试这个:
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: null -->
<!-- 语言: lang-js -->
// 禁用日期
var input = document.getElementById("preDate");
var today = new Date();
var day = today.getDate() + 1;
var mon = new String(today.getMonth() + 1); // 一月是0!
var yr = today.getFullYear();
if (mon.length > 0) {
mon = `0${mon}`;
}
if (day < 10) {
day = `0${day}`;
}
var date = `${yr}-${mon}-${day}`;
input.disabled = false;
input.setAttribute('min', date);
input.value = date;
console.log(date);
<!-- 语言: lang-html -->
<div class="form-group col-3 rfdate">
<label class="col-sm-6 control-label p-sm-0">退款日期</label>
<input type="date" class="form-control" id="preDate" name="preDate" />
</div>
<!-- 结束代码片段 -->
英文:
Try this:
<!-- begin snippet: js hide: false console: true babel: null -->
<!-- language: lang-js -->
// Disable date
var input = document.getElementById("preDate");
var today = new Date();
var day = today.getDate()+1;
var mon = new String(today.getMonth()+1); //January is 0!
var yr = today.getFullYear();
if (mon.length > 0) {
mon = `0${mon}`;
}
if (day < 10) {
day = `0${day}`;
}
var date = `${yr}-${mon}-${day}`;
input.disabled = false;
input.setAttribute('min', date);
input.value = date;
console.log(date);
<!-- language: lang-html -->
<div class="form-group col-3 rfdate">
<label class="col-sm-6 control-label p-sm-0">Refund Date</label>
<input type="date" class="form-control" id="preDate" name="preDate" />
</div>
<!-- end snippet -->
答案2
得分: 1
明天的日期是这样创建的:
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
英文:
Tomorrows date is smart created like that:
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论