【问题标题】:Datetime-local limit past and future date JavaScriptDatetime-local 限制过去和未来的日期 JavaScript
【发布时间】:2022-10-18 20:16:25
【问题描述】:

我有 2 个本地日期时间输入(开始时间和结束时间)。我想将开始日期限制为仅选择我所做的最后 72 小时,但我不能限制未来的时间。我只想要今天,而且只持续 3 天。仅在今天和接下来的 3 天结束时间。 这是我的代码:

<input type="datetime-local" name="start_timestamp" id="start_timestamp" required>
            
<input type="datetime-local" name="end_timestamp" id="end_timestamp" required>

<script>
    //Do not let to select START TIME  in the PAST
    var today = new Date();
    var past = new Date(today.setDate(today.getDate() - 3)).toISOString().slice(0, 16);
    

    document.getElementsByName("start_timestamp")[0].min = past;
    document.getElementsByName("start_timestamp")[0].max = today;
</script>


            

【问题讨论】:

    标签: javascript html forms frontend


    【解决方案1】:

    请参阅此处可能的修复。

    <input type="datetime-local" name="start_timestamp" id="start_timestamp" required>
                
    <input type="datetime-local" name="end_timestamp" id="end_timestamp" required>
    
    
    
    
    <script>
        //Do not let to select START TIME  in the PAST
        let today = new Date();
        let past = new Date(today.setDate(today.getDate() - 3)).toISOString().slice(0, 16);
        today = new Date().toISOString().slice(0, 16);
    
        document.getElementsByName("start_timestamp")[0].min = past;
        document.getElementsByName("start_timestamp")[0].max = today;
        
        document.getElementsByName("end_timestamp")[0].min = past;
        document.getElementsByName("end_timestamp")[0].max = today;
        
    </script>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-14
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      • 2015-05-23
      • 2017-10-23
      • 2022-07-06
      • 1970-01-01
      相关资源
      最近更新 更多