如何在JavaScript中增加日期并禁用之前的日期?

huangapple go评论103阅读模式
英文:

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(&quot;preDate&quot;);
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 &lt; 0) {
  mon = &quot;0&quot; + mon;
}

var date = new String(yr + &#39;-&#39; + mon + &#39;-&#39; + day);

input.disabled = false;
input.setAttribute(&#39;min&#39;, date);

<!-- language: lang-html -->

&lt;div class=&quot;form-group col-3 rfdate&quot;&gt;
  &lt;label class=&quot;col-sm-6 control-label p-sm-0&quot;&gt;Refund Date&lt;/label&gt;
  &lt;input type=&quot;date&quot; class=&quot;form-control&quot; id=&quot;preDate&quot; name=&quot;preDate&quot; /&gt;
&lt;/div&gt;

<!-- 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(&quot;preDate&quot;);
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 &gt; 0) {
  mon = `0${mon}`;
}
if (day &lt; 10) {
  day = `0${day}`;
}

var date = `${yr}-${mon}-${day}`;

input.disabled = false;
input.setAttribute(&#39;min&#39;, date);
input.value = date;
console.log(date);

<!-- language: lang-html -->

&lt;div class=&quot;form-group col-3 rfdate&quot;&gt;
  &lt;label class=&quot;col-sm-6 control-label p-sm-0&quot;&gt;Refund Date&lt;/label&gt;
  &lt;input type=&quot;date&quot; class=&quot;form-control&quot; id=&quot;preDate&quot; name=&quot;preDate&quot; /&gt;
&lt;/div&gt;

<!-- 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);

huangapple
  • 本文由 发表于 2020年1月3日 19:56:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定