javascript - How to set the default date to two days from current date using date picker? - Stack Overflow

I went through many examples but Im not able to set the value of my date picker. The textbox value is s

I went through many examples but Im not able to set the value of my date picker. The textbox value is shown as 01/01/2016 and the calendar opens to that date as well. Im trying to set the default date to two days from now in the text box as well as in the calendar that shows up on click. I tried the following:

1.

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

2.

$(".date-picker").datepicker().datepicker('setDate', '+2');

My HTML is:

<input type="text" class="input date-picker">

I agree this question has been repeated lots of times but I have tried most of the examples but still Im not able to make it work. Please advice.

I went through many examples but Im not able to set the value of my date picker. The textbox value is shown as 01/01/2016 and the calendar opens to that date as well. Im trying to set the default date to two days from now in the text box as well as in the calendar that shows up on click. I tried the following:

1.

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

2.

$(".date-picker").datepicker().datepicker('setDate', '+2');

My HTML is:

<input type="text" class="input date-picker">

I agree this question has been repeated lots of times but I have tried most of the examples but still Im not able to make it work. Please advice.

Share Improve this question edited Feb 23, 2015 at 18:35 Lumi Lu 3,3051 gold badge13 silver badges21 bronze badges asked Feb 23, 2015 at 18:29 palpal 1052 gold badges4 silver badges17 bronze badges 3
  • possible duplicate of jQuery UI Datepicker - setDate not working – abaracedo Commented Feb 23, 2015 at 18:32
  • Take a look at stackoverflow./questions/3818193/… and put the someDate-variable or the someFormattedDate variable where new Date() is right now – bestprogrammerintheworld Commented Feb 23, 2015 at 18:35
  • possible duplicate of Jquery UI datepicker default date – talemyn Commented Feb 23, 2015 at 18:35
Add a ment  | 

3 Answers 3

Reset to default 4

The best way to do this is to extend the javascript date function using prototype

Date.prototype.addDays = function(days) {
this.setDate(this.getDate() + days);
return this;
};

Then simply call as follows:-

$(function() {
var currentDate = new Date();

var myDate = currentDate.addDays(2);

$(".date-picker").datepicker(); //initialise
$(".date-picker").datepicker('setDate', myDate); //set date

});

DONT FORGET you have to INITIALISE the datepicker - then set the date

JS Fiddle here

Initialize a datepicker with the defaultDate option specified.

$(document).ready(function () {
    $(".date-picker").datepicker({
        defaultDate: +2,
        dateFormat: "mm/dd/yy"
    });
});

http://jsfiddle/8ejbw8d5/

$( ".date-picker" ).datepicker({
  defaultDate: +2
});
// Getter
var defaultDate = $( ".selector" ).datepicker( "option", "defaultDate" );

// Setter
$( ".selector" ).datepicker( "option", "defaultDate", +2 );

Read more here: http://api.jqueryui./datepicker/#option-defaultDate

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744219534a4563713.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信