gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:04:36 26 4
gpt4 key购买 nike

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

例如

当前日期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">

最佳答案

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

$(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">

关于javascript - 如何按时间禁用当前日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49851093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com