设置日期输入字段的最小日期和最大日期为今天加30天。

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

Set date input field's min and max 30days date to today

问题

我只有这样简单的一行代码:
有没有办法使用本地日期禁用以下范围?

  1. <input type='date' min='2023-06-02' max='2023-07-02'></input>

有没有简单的方法将最小日期设置为“今天”,并将最大日期设置为+30天?还是我必须使用JavaScript来做这个?可以选择的示例日期是从今天到下一个日期的+30天。

英文:

I just have a simple line of code like this:
Is there a way to get disable the following range using the local date?


&lt;input type=&#39;date&#39; min=&#39;2023-06-02&#39; max=&#39;2023-07-02&#39;&gt;&lt;/input&gt;

Is there a simple way to set the min date to "today" and set the max date to +30 days? Or do I have to use Javascript to do this?

example date the can be selected will be todays date to +30 days of the following date

答案1

得分: 1

使用JavaScript,首先需要通过以下方式获取今天的日期:

  1. const [today] = new Date().toISOString().split('T');

然后获取最大日期:

  1. const maxDate = new Date();
  2. maxDate.setDate(maxDate.getDate() + 30);
  3. const [maxDateFormatted] = maxDate.toISOString().split('T');

接下来,设置 minmax 属性:

  1. const dateInput = document.getElementById('myDateInput');
  2. dateInput.setAttribute('min', today);
  3. dateInput.setAttribute('max', maxDateFormatted);

这是一个工作示例:

  1. <!-- begin snippet: js hide: false console: true babel: false -->
  2. <!-- language: lang-js -->
  3. const [today] = new Date().toISOString().split('T');
  4. const maxDate = new Date();
  5. maxDate.setDate(maxDate.getDate() + 30);
  6. const [maxDateFormatted] = maxDate.toISOString().split('T');
  7. const dateInput = document.getElementById('myDateInput');
  8. dateInput.setAttribute('min', today);
  9. dateInput.setAttribute('max', maxDateFormatted);
  10. <!-- language: lang-html -->
  11. <input type="date" id="myDateInput"></input>
  12. <!-- end snippet -->
英文:

With javascript, first, you need to get today's date by

  1. const [today] = new Date().toISOString().split(&#39;T&#39;)

Then get the max date

  1. const maxDate = new Date();
  2. maxDate.setDate(maxDate.getDate() + 30);
  3. const [maxDateFormatted] = maxDate.toISOString().split(&#39;T&#39;);

Then set the min and max attributes.

  1. const dateInput = document.getElementById(&#39;myDateInput&#39;);
  2. dateInput.setAttribute(&#39;min&#39;, today);
  3. dateInput.setAttribute(&#39;max&#39;, maxDateFormatted);

This is working example

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const [today] = new Date().toISOString().split(&#39;T&#39;);
  2. const maxDate = new Date();
  3. maxDate.setDate(maxDate.getDate() + 30);
  4. const [maxDateFormatted] = maxDate.toISOString().split(&#39;T&#39;);
  5. const dateInput = document.getElementById(&#39;myDateInput&#39;);
  6. dateInput.setAttribute(&#39;min&#39;, today);
  7. dateInput.setAttribute(&#39;max&#39;, maxDateFormatted);

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

  1. &lt;input type=&quot;date&quot; id=&quot;myDateInput&quot;&gt;&lt;/input&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月2日 15:06:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387889.html
匿名

发表评论

匿名网友

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

确定