简体   繁体   English

html 日期形式:如何使用 Javascript 将开始日期设置为明天

[英]html date form: how to set start date as tomorrow using Javascript

Trying to limit the start date as tomorrow (local date).试图将开始日期限制为明天(本地日期)。 Can someone tell why below code isn't working:有人可以告诉为什么下面的代码不起作用:

  <form method="POST">
       <div>
            <label for="s2">pickup_date</label>
            <input type = 'date'  name='pickup_date' required>
         <br /><br />
        </div>
  </form>

    <script>
                var time = new Date();
                var localTimeStr = time.toLocaleString('en-US', { timeZone: 'Asia/Shanghai' });
                today = new Date(localTimeStr)
                tomorrow = new Date(today.setDate(today.getDate() + 1)).toISOString().split('T')[0];
                t = String(tomorrow)
                document.getElementsByName("pickup_date")[0].setAttribute('min', t);
    </script>

the output of below code is 2022-01-18:下面代码的 output 是 2022-01-18:

  var time = new Date();
                var localTimeStr = time.toLocaleString('en-US', { timeZone: 'Asia/Shanghai' });
                today = new Date(localTimeStr)
                tomorrow = new Date(today.setDate(today.getDate() + 1)).toISOString().split('T')[0];
                t = tomorrow.split('-')[0]+'-'+tomorrow.split('-')[1]+'-'+tomorrow.split('-')[2]
                console.log(t)

however, the calendar start date in the form is still 2022-01-17.但是,表格中的日历开始日期仍然是 2022-01-17。 But, when I manually set但是,当我手动设置

document.getElementsByName("pickup_date")[0].setAttribute('min', '2022-01-18');

the calendar start from 2022-01-18 correctly.日历正确地从 2022-01-18 开始。 why!为什么!

So, this is your code:所以,这是你的代码:

 var time = new Date(); var localTimeStr = time.toLocaleString('en-US', { timeZone: 'Asia/Shanghai' }); today = new Date(localTimeStr) tomorrow = new Date(today.setDate(today.getDate() + 1)).toISOString().split('T')[0]; t = String(tomorrow) document.getElementsByName("pickup_date")[0].setAttribute('min', t);
 <form method="POST"> <div> <label for="s2">pickup_date</label> <input type = 'date' name='pickup_date' required> <br /><br /> </div> </form>

Make a cup of tea and look again.泡一杯茶再看一遍。 Seems to be working as expected, right?似乎按预期工作,对吧?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM