如何按时间禁用当前日期?

How to disable current date by time?

我在我的网站中使用了输入日期类型。我想按时间禁用当前日期。

例如

12:00:00 PM 当前日期 2018-04-16 之后禁用。

我试过这段代码:

<!-- disable past date -->
$(function() {
  var dtToday = new Date();

  var month = dtToday.getMonth() + 1;
  var day = dtToday.getDate();
  var year = dtToday.getFullYear();
  if (month < 10)
    month = '0' + month.toString();
  if (day < 10)
    day = '0' + day.toString();

  var maxDate = year + '-' + month + '-' + day;
  //console.log(maxDate);
  $('#demo_date').attr('min', maxDate);
  $('#demo_date').attr('max', '2018-04-29');
});
<!-- disable past date -->

<!-- disable date by time -->
$(document).ready(function() {
  var time = new Date().toLocaleTimeString();
  if (time <= '12:00:00 PM') {

    // code for disable demo_date
  }
});

<!-- disable date by time -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input placeholder="Select Date" class="textbox-n" type="text" name="demo_date" onfocus="(this.type='date')" id="demo_date">

浏览器控制台的结果如下:

>> var time = new Date().toLocaleTimeString();
undefined
>> time
"11:27:28 AM"
>> time <= '12:00:00 PM'
true

代码有效 fine.But 您应该定义表达式的其他部分。你应该定义:

if( time >= some_date_value && time <= some_date_value ) 
{
    // code for disable demo_date
} 

好的,你明白了。我在这里为您做了一个演示,在 12:00 下午之后您将无法 select 当前日期。干杯....

$(function() {
  var dtToday = new Date();

  var month = dtToday.getMonth() + 1;
  var day = dtToday.getDate();
  var year = dtToday.getFullYear();
 
 
  if (month < 10)
    month = '0' + month.toString();
  if (day < 10)
    day = '0' + day.toString();

  var maxDate = year + '-' + month + '-' + day;
  //console.log(maxDate);
 
   var time = new Date().toLocaleTimeString();
  if (time >= '12:00:00 PM') {
 day = day+1
 maxDate = year + '-' + month + '-' + day;
    // code for disable demo_date
  }
 
  
  
  $('#demo_date').attr('min', maxDate);
  $('#demo_date').attr('max', '2018-04-29');
});
<!-- disable past date -->

<!-- disable date by time -->
$(document).ready(function() {
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input placeholder="Select Date" class="textbox-n" type="text" name="demo_date" onfocus="(this.type='date')" id="demo_date">