将今天的日期设置为 jQuery UI datepicker 中的默认日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4915990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set today's date as default date in jQuery UI datepicker
提问by Chris Abrams
I just want today's date to be the default value in the input that is using jQuery UI's datepicker
:
我只希望今天的日期是使用 jQuery UI 的输入中的默认值datepicker
:
<input id="mydate" type="text" />
I tried the below code but it didn't work:
我尝试了下面的代码,但没有用:
var currentDate = new Date();
$("#mydate").datepicker("setDate",currentDate);
回答by Martin Eden
You need to make the call to setDate separately from the initialization call. So to create the datepicker and set the date in one go:
您需要与初始化调用分开调用 setDate 。因此,要一次性创建日期选择器并设置日期:
$("#mydate").datepicker().datepicker("setDate", new Date());
Why it is like this I do not know. If anyone did, that would be interesting information.
为什么会这样我不知道。如果有人这样做了,那将是有趣的信息。
回答by T J
You have to initialize the datepickerbefore calling a datepickermethod
Here is a
这里有一个
Working JSFiddle.
工作 JSFiddle。
$("#mydate").datepicker().datepicker("setDate", new Date());
//-initialization--^ ^-- method invokation
<link href="https://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<input type="text" id="mydate" />
P.S: This assumes that you have the correct date set in your computer
PS:这假设您在计算机中设置了正确的日期
回答by rjony
Its very simple you just add this script,
很简单,你只需添加这个脚本,
$("#mydate").datepicker({ dateFormat: "yy-mm-dd"}).datepicker("setDate", new Date());
Here, setDate set today date & dateFormat define which format you want set or show.
在这里, setDate 设置今天日期和 dateFormat 定义您要设置或显示的格式。
Hope its simple script work..
希望它简单的脚本工作..
回答by Ashisha Nautiyal
$("#date").datepicker.regional[""].dateFormat = 'dd/mm/yy';
$("#date").datepicker("setDate", new Date());
Always work for me
永远为我工作
回答by Usman Younas
This one work for me , first you bind datepicker to text-box and in next line set (today as default date) the date to it
这对我有用,首先将日期选择器绑定到文本框,然后在下一行设置(今天为默认日期)日期
$("#date").datepicker();
$("#date").datepicker("setDate", new Date());
回答by Wilzon
Note: When you pass setDate, you are calling a method which assumes the datepicker has already been initialized on that object.
注意:当您传递 setDate 时,您正在调用一个方法,该方法假定日期选择器已在该对象上初始化。
$(function() {
$('#date').datepicker();
$('#date').datepicker('setDate', '04/23/2014');
});
回答by Morten Holmgaard
Set default date in initialization:
在初始化中设置默认日期:
$("#date").datepicker({
defaultDate: '01/26/2014'
});
回答by Jan
This worked for me and also localised it:
这对我有用并对其进行了本地化:
$.datepicker.setDefaults( $.datepicker.regional[ "fr" ] );
$(function() {
$( "#txtDespatchDate" ).datepicker( );
$( "#txtDespatchDate" ).datepicker( "option", "dateFormat", "dd/mm/yy" );
$('#txtDespatchDate').datepicker('setDate', new Date());
});
回答by Mike Nguyen
Just set minDate: 0
只需设置 minDate: 0
Here is my script:
这是我的脚本:
$("#valid_from")
.datepicker({
minDate: 0,
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 3
})
回答by Radha Anil K
I tried many ways and came up with my own solution which works absolutely as required.
我尝试了很多方法并提出了我自己的解决方案,该解决方案绝对按要求工作。
"mydate" is the ID of input or datepicker element/control.
“mydate”是输入或日期选择器元素/控件的 ID。
$(document).ready(function () {
var dateNewFormat, onlyDate, today = new Date();
dateNewFormat = today.getFullYear() + '-' + (today.getMonth() + 1);
onlyDate = today.getDate();
if (onlyDate.toString().length == 2) {
dateNewFormat += '-' + onlyDate;
}
else {
dateNewFormat += '-0' + onlyDate;
}
$('#mydate').val(dateNewFormat);
});