将今天的日期设置为jQuery UI datepicker的默认日期[复制]

时间:2022-11-30 08:59:09

This question already has an answer here:

这个问题已经有了答案:

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);

13 个解决方案

#1


74  

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。因此,创建datepicker并将日期设置为:

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

Why it is like this I do not know. If anyone did, that would be interesting information.

我不知道为什么会这样。如果有人做了,那将是有趣的信息。

#2


67  

You have to initialize the datepicker before calling a datepicker method

您必须在调用datepicker方法之前初始化datepicker

Here is a

这是一个

Working 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

P。S:这是假设你的电脑里设置了正确的日期

#3


18  

Its very simple you just add this script,

你只需添加这个脚本,

$("#mydate").datepicker({ dateFormat: "yy-mm-dd"}).datepicker("setDate", "0");

Here, setDate set today date & dateFormat define which format you want set or show.

这里,setDate set today date & dateFormat定义您想要设置或显示的格式。

Hope its simple script work..

希望它的简单脚本可以工作。

#4


5  

$("#date").datepicker.regional[""].dateFormat = 'dd/mm/yy';
$("#date").datepicker("setDate", new Date());

Always work for me

总是为我工作

#5


3  

Note: When you pass setDate, you are calling a method which assumes the datepicker has already been initialized on that object.

注意:当您传递setDate时,您正在调用一个方法,该方法假定datepicker已经在该对象上初始化。

$(function() {    
   $('#date').datepicker();
   $('#date').datepicker('setDate', '04/23/2014');
});

Test: http://jsfiddle.net/wimarbueno/hQkec/1/

测试:http://jsfiddle.net/wimarbueno/hQkec/1/

#6


2  

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

这个对我来说是可行的,首先将datepicker绑定到文本框,然后在下一行中(今天是默认日期)将日期绑定到它

$("#date").datepicker();

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

#7


2  

Set default date in initialization:

在初始化时设置默认日期:

$("#date").datepicker({
    defaultDate: '01/26/2014'
});

#8


1  

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());
});

#9


1  

Just set minDate: 0

只是设置minDate:0

Here is my script:

这是我的脚本:

$("#valid_from")
   .datepicker({
     minDate: 0,
     defaultDate: "+1w",
     changeMonth: true,
     numberOfMonths: 3
})

#10


0  

You have not defined

你没有定义

$("#mydate").datepicker({});

#11


0  

Try this:

试试这个:

$('.datepicker').datepicker('update', new Date(year,month,day));

#12


0  

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”是输入或datepicker元素/控件的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);
    });

#13


-1  

try this:

试试这个:

$("#mydate").datepicker("setDate",'1d');

#1


74  

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。因此,创建datepicker并将日期设置为:

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

Why it is like this I do not know. If anyone did, that would be interesting information.

我不知道为什么会这样。如果有人做了,那将是有趣的信息。

#2


67  

You have to initialize the datepicker before calling a datepicker method

您必须在调用datepicker方法之前初始化datepicker

Here is a

这是一个

Working 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

P。S:这是假设你的电脑里设置了正确的日期

#3


18  

Its very simple you just add this script,

你只需添加这个脚本,

$("#mydate").datepicker({ dateFormat: "yy-mm-dd"}).datepicker("setDate", "0");

Here, setDate set today date & dateFormat define which format you want set or show.

这里,setDate set today date & dateFormat定义您想要设置或显示的格式。

Hope its simple script work..

希望它的简单脚本可以工作。

#4


5  

$("#date").datepicker.regional[""].dateFormat = 'dd/mm/yy';
$("#date").datepicker("setDate", new Date());

Always work for me

总是为我工作

#5


3  

Note: When you pass setDate, you are calling a method which assumes the datepicker has already been initialized on that object.

注意:当您传递setDate时,您正在调用一个方法,该方法假定datepicker已经在该对象上初始化。

$(function() {    
   $('#date').datepicker();
   $('#date').datepicker('setDate', '04/23/2014');
});

Test: http://jsfiddle.net/wimarbueno/hQkec/1/

测试:http://jsfiddle.net/wimarbueno/hQkec/1/

#6


2  

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

这个对我来说是可行的,首先将datepicker绑定到文本框,然后在下一行中(今天是默认日期)将日期绑定到它

$("#date").datepicker();

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

#7


2  

Set default date in initialization:

在初始化时设置默认日期:

$("#date").datepicker({
    defaultDate: '01/26/2014'
});

#8


1  

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());
});

#9


1  

Just set minDate: 0

只是设置minDate:0

Here is my script:

这是我的脚本:

$("#valid_from")
   .datepicker({
     minDate: 0,
     defaultDate: "+1w",
     changeMonth: true,
     numberOfMonths: 3
})

#10


0  

You have not defined

你没有定义

$("#mydate").datepicker({});

#11


0  

Try this:

试试这个:

$('.datepicker').datepicker('update', new Date(year,month,day));

#12


0  

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”是输入或datepicker元素/控件的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);
    });

#13


-1  

try this:

试试这个:

$("#mydate").datepicker("setDate",'1d');