Jquery DatePicker 设置默认日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14580749/
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
Jquery DatePicker Set default date
提问by maaz
I Have two date fields where I use DatePicker to pick the dates.
我有两个日期字段,我使用 DatePicker 来选择日期。
For the first date field, I want today's date as the default date.
For the second date field, I need today + 15
days as my default date
对于第一个日期字段,我希望今天的日期作为默认日期。
对于第二个日期字段,我需要today + 15
几天作为我的默认日期
jQuery
jQuery
$("[name=trainingStartFromDate]").datepicker({ dateFormat: 'dd-mm-yy', changeYear: true});
$("[name=trainingStartToDate]").datepicker({ dateFormat: 'dd-mm-yy', changeYear: true});
How do I set the default dates?
如何设置默认日期?
I have tried setDate: new Date()
for first date but it's not working.
我试过setDate: new Date()
第一次约会,但没有用。
回答by gdoron is supporting Monica
Today date:
今天日期:
$( ".selector" ).datepicker( "setDate", new Date());
// Or on the init
$( ".selector" ).datepicker({ defaultDate: new Date() });
15 days from today:
从今天起 15 天:
$( ".selector" ).datepicker( "setDate", 15);
// Or on the init
$( ".selector" ).datepicker({ defaultDate: 15 });
回答by Ajit Singh
To create the datepicker and set the date.
创建日期选择器并设置日期。
$('.next_date').datepicker({ dateFormat: 'dd-mm-yy'}).datepicker("setDate", new Date());
回答by bipen
use defaultDate()
Set the date to highlight on first opening if the field is blank. Specify either an actual date via a Date object or as a string in the current [[UI/Datepicker#option-dateFormat|dateFormat]], or a number of days from today (e.g. +7) or a string of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, e.g. '+1m +7d'), or null for today.
如果该字段为空,则将日期设置为在首次打开时突出显示。通过 Date 对象或作为当前 [[UI/Datepicker#option-dateFormat|dateFormat]] 中的字符串指定实际日期,或从今天起的天数(例如 +7)或一串值和句点( 'y' 表示年,'m' 表示月,'w' 表示周,'d' 表示天,例如 '+1m +7d'),或者今天的 null。
try this
尝试这个
$("[name=trainingStartFromDate]").datepicker({ dateFormat: 'dd-mm-yy', changeYear: true,defaultDate: new Date()});
$("[name=trainingStartToDate]").datepicker({ dateFormat: 'dd-mm-yy', changeYear: true,defaultDate: +15});
回答by Meena
For today's Date
对于今天的日期
$(document).ready(function() {
$('#textboxname').datepicker();
$('#textboxname').datepicker('setDate', 'today');});
回答by Haisum Usman
回答by jayesh dhameliya
<script type="text/javascript">
$(document).ready(function () {
$("#txtDate").datepicker({ dateFormat: 'yy/mm/dd' }).datepicker("setDate", "0");
$("#txtDate2").datepicker({ dateFormat: 'yy/mm/dd', }).datepicker("setDate", new Date().getDay+15); }); </script>
回答by Radha Anil K
Code to display current date in element input or datepicker with ID="mydate"
在 ID="mydate" 的元素输入或日期选择器中显示当前日期的代码
Don't forget add reference to jquery-ui-*.js
不要忘记添加对 jquery-ui-*.js 的引用
$(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);
});