Dev Dev - 6 months ago 94
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....

http://www.yatra.com/hotels.html

can anyone suggest me,how to do this??

I have used same datepickers as shown in link..

thanks in advance.

Answer

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

$('#your-selector')
    .datepicker('setDate',new Date());

of a uninitialized datepicker use:

$('#your-selector')
    .datepicker().datepicker('setDate',new Date());

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

var today = new Date();
var twoDaysBefore = new Date();
twoDaysBefore.setDate(today.getDate()-2);

and set the date

$('#your-selector')
    .datepicker('setDate', twoDaysBefore);

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

var getDate = function(daysDelta) {
    var d = new Date();
    (d.setDate(d.getDate()+daysDelta));
    return  d;
}

console.log(getDate(-3))

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