Dev Dev - 2 years ago 229
PHP Question

How to set current date of datepicker to textbox value

I am developing application for hotel booking..
I have used 2 datepickers for selecting check-in & check-out date..

But now i want my first date-picker's current value to be shown on that textbox by default & another textbox of second datepicker to be filled by date of 2 days after...

Just like these datepickers....

can anyone suggest me,how to do this??

I have used same datepickers as shown in link..

thanks in advance.

Answer Source

You can set a date like this (if the datepicker is initialized)

    .datepicker('setDate',new Date());

of a uninitialized datepicker use:

    .datepicker().datepicker('setDate',new Date());

to create a date 2 days before... make this:

var today = new Date();
var twoDaysBefore = new Date();

and set the date

    .datepicker('setDate', twoDaysBefore);

you can create a function if you need this algorithm more times:

var getDate = function(daysDelta) {
    var d = new Date();
    return  d;


without jQuery (thanks), format your date object like this:

var d = getDate(-2);
var yyyy = d.getFullYear().toString();
var mm = (d.getMonth()+1).toString();
var dd  = d.getDate().toString();
var dateAsString =  yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" + (dd[1]?dd:"0"+1);
document.getElementById('id').setAttribute('value', dateAsString);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download