datetimepicker 输入自动填充当前日期并禁用当前日期之前的天数

发布于 2025-01-16 12:43:22 字数 1102 浏览 1 评论 0原文

我有一个日期时间选择器输入,我希望输入自动填充当前日期,但用户仍然可以更改输入中的日期。

我还想禁用当前日期之前的日子。

我尝试使用 var Today = new Date(); $('#datetimepicker1').value = Today;,但是没有用。当我警告该值时,它仍然显示日期未定义。

我也尝试过使用

var today = new Date();
$('#datetimepicker1').datepicker({
    format: 'dd/mm/yyyy',
    defaultDate: today
});

,但它只显示这样的日期表 在此处输入图像描述

所需的输出是 datetimepicker 自动填充当前日期,并且禁用当前日期之前的天数。 此外,日期时间选择器输入会自动更新为所选日期的值。 输入图片此处的描述填写所选日期。日期表显示单击日历图标的时间。

我的html代码:

<div class='input-group date' id='datetimepicker1'>
    <input type="date" class="btn isi datetime px-3 py-1" id="datetime" name="date" placeholder="3/22/2022">
        <span class="input-group-addon">
            <span class="glyphicon glyphicon-calendar" onclick="date()"></span>
        </span>
</div>

i have a datetimepicker input, i want the input automatically filled with current date, but user still can change the date in the input.

i also want to disable the days before current date.

i've tried to use var today = new Date(); $('#datetimepicker1').value = today;, but it didn't work. When i alert the value, it still shows that the date is undefined.

i've also tried to use

var today = new Date();
$('#datetimepicker1').datepicker({
    format: 'dd/mm/yyyy',
    defaultDate: today
});

but it only show table of dates like this enter image description here

the desired output is datetimepicker automatically filled with current date and the days before current date is disabled.
also, the datetimepicker input automatically updated with the selected date's value. enter image description herefilled with selected date. the date table shows when the calendar icon clicked.

my html code:

<div class='input-group date' id='datetimepicker1'>
    <input type="date" class="btn isi datetime px-3 py-1" id="datetime" name="date" placeholder="3/22/2022">
        <span class="input-group-addon">
            <span class="glyphicon glyphicon-calendar" onclick="date()"></span>
        </span>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流心雨 2025-01-23 12:43:24
$(function(){
$("#tgl").datepicker({
autoclose: true,
todayHighlight: true,
startDate: "dateToday",
format:'yyyy-mm-dd'
});
}):

'''

你可以添加属性starDate

$(function(){
$("#tgl").datepicker({
autoclose: true,
todayHighlight: true,
startDate: "dateToday",
format:'yyyy-mm-dd'
});
}):

'''

you can add atribute starDate

茶花眉 2025-01-23 12:43:24

试试这个
"minDate": 0, 禁用当前日期之前的天数,"setDate": new Date() 自动填充当前日期。

$("#datepicker").datepicker(
{
  "minDate": 0,
   onSelect: function() { 
        var dateObject = $(this).datepicker('getDate'); 
        console.log(dateObject);
    } 
});




$("#datepicker").datepicker('setDate', new Date());


 var obj = $("#datepicker").datepicker('getDate'); 
 console.log(obj);
<!-- load jQuery and jQuery UI -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

<!-- load jQuery UI CSS theme -->
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">

<!-- the datepicker input -->
<input type='text' id='datepicker' placeholder='Select date' />

try this
"minDate": 0, to disable the days before current date and "setDate": new Date() automatically filled with current date.

$("#datepicker").datepicker(
{
  "minDate": 0,
   onSelect: function() { 
        var dateObject = $(this).datepicker('getDate'); 
        console.log(dateObject);
    } 
});




$("#datepicker").datepicker('setDate', new Date());


 var obj = $("#datepicker").datepicker('getDate'); 
 console.log(obj);
<!-- load jQuery and jQuery UI -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

<!-- load jQuery UI CSS theme -->
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">

<!-- the datepicker input -->
<input type='text' id='datepicker' placeholder='Select date' />

梦里°也失望 2025-01-23 12:43:23

setDate 与当前日期将生成一个预选日期,即今天,minDate 0 禁用以前的日期。分钟:当前日期也有效。

$('#datepicker').datepicker({
    "setDate": new Date(),
    "autoclose": true,
    "minDate": 0,
});

setDate with the current date in will generate a preselected date that is today, minDate 0 disables previous dates. min:current_date also works.

$('#datepicker').datepicker({
    "setDate": new Date(),
    "autoclose": true,
    "minDate": 0,
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文