Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery日期选择器设置默认日期_Jquery_Jquery Ui_Jquery Ui Datepicker - Fatal编程技术网

Jquery日期选择器设置默认日期

Jquery日期选择器设置默认日期,jquery,jquery-ui,jquery-ui-datepicker,Jquery,Jquery Ui,Jquery Ui Datepicker,我有两个日期字段,我使用DatePicker来选择日期 对于第一个日期字段,我希望今天的日期作为默认日期。 对于第二个日期字段,我需要今天+15天作为默认日期 jQuery $("[name=trainingStartFromDate]").datepicker({ dateFormat: 'dd-mm-yy', changeYear: true}); $("[name=trainingStartToDate]").datepicker({ dateFormat: 'dd-mm-yy', cha

我有两个日期字段,我使用DatePicker来选择日期

对于第一个日期字段,我希望今天的日期作为默认日期。
对于第二个日期字段,我需要
今天+15天作为默认日期

jQuery

$("[name=trainingStartFromDate]").datepicker({ dateFormat: 'dd-mm-yy', changeYear: true});
$("[name=trainingStartToDate]").datepicker({ dateFormat: 'dd-mm-yy', changeYear: true}); 
如何设置默认日期

我尝试了
setDate:new Date()
作为第一个日期,但它不起作用。

使用

如果字段为空,则设置第一次打开时高亮显示的日期。通过日期对象指定实际日期或当前[[UI/Datepicker#option dateFormat | dateFormat]]中的字符串,或从今天算起的天数(例如+7)或一组值和期间(“y”表示年,“m”表示月,“w”表示周,“d”表示天,例如+1m+7d”),或今天为空

试试这个

$("[name=trainingStartFromDate]").datepicker({ dateFormat: 'dd-mm-yy', changeYear: true,defaultDate: new Date()});
$("[name=trainingStartToDate]").datepicker({ dateFormat: 'dd-mm-yy', changeYear: true,defaultDate: +15}); 
今日日期:

$( ".selector" ).datepicker( "setDate", new Date());
// Or on the init
$( ".selector" ).datepicker({ defaultDate: new Date() });
从今天起15天:

$( ".selector" ).datepicker( "setDate", 15);
// Or on the init
$( ".selector" ).datepicker({ defaultDate: 15 });

创建日期选择器并设置日期

$('.next_date').datepicker({ dateFormat: 'dd-mm-yy'}).datepicker("setDate", new Date());

首先,您需要获取当前日期

var currentDate = new Date();
然后您需要将其放入datepicker的参数中,如下所示

$("#datepicker").datepicker("setDate", currentDate);
检查以下内容。

查看今天的日期

$(document).ready(function() {
$('#textboxname').datepicker();
$('#textboxname').datepicker('setDate', 'today');});

$(文档).ready(函数(){
$(“#txtDate”).datepicker({dateFormat:'yy/mm/dd'}).datepicker(“setDate”,“0”);
$(“#txtDate2”).datepicker({dateFormat:'yy/mm/dd',}).datepicker(“setDate”,new Date().getDay+15);});

在元素输入或日期选择器中使用ID=“mydate”显示当前日期的代码

别忘了添加对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);
    });

我在文本框中显示相同的点,但不在文本框中calendar@KushalJain,如果您看到这一点,很可能是因为您尚未初始化日期选择器。必须先运行“$(“.selector”).datepicker()”,然后才能运行“$(.selector”).datepicker(“setDate”,new Date());”
    $(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);
    });