views:

292

answers:

2

I have a form text field that pulls a date from a database (in the format yyyy-mm-dd).

<input class="" type="text" name="closingDate" id="closingDate" value="<?php echo $closingdate;?>" />@ Midday

I then have a jQuery datepicker that will grab its 'defaultDate' value as the value attribute of the input shown above.

$(document).ready(function(){
 $("#datepicker").datepicker({defaultDate: 'input#closingDate.attr('value')' , altField: 'input#closingDate' , altFormat: 'yy-mm-dd'});
});

(plz note: yy-mm-dd is correct to format as yyyy-mm-dd, also $closingdate correctly echo's out as yyyy-mm-dd as i have checked it elsewhere in the page).

However the datepicker will not grab the date from the input field, it simply defaults to its 'today' value.

Any help on this appreciated. thx in advance.

A: 

You shouldn't be putting your default date inside a literal string, the .attr() function returns a string:

$(document).ready(function(){
    $("#datepicker").datepicker({
        defaultDate: $('input#closingDate').attr('value'),
        altField: $('input#closingDate'),
        altFormat: 'yy-mm-dd'
    });
});
Soviut
Hi Soviut, thx for your reply.i have tried your code, it does register a date... however it lists the wrong date - i know the database value has the date '2010-01-01', the code above outputs '2015-05-12' in the text field, although it correctly displays this wrong on the datepicker :Salmost there i reckon, any ideas?
ab24
Its probably just a date formatting issue. Look into the Javascript Date object for how to easily reformat a date to whatever you need.
Soviut
Hey Soviut, i have tried to edit the date format but to no avail. any more ideas?
ab24
A: 

i know this is a bit late but i had the exact same problem when trying to set the default date in the JQuery UI DatePicker.

It kept taking my asp.net DateTime and adding random years to it or something.

My solution :

var myDate= new Date('<%=myObject.myDate.ToLongDateString() %>');
$("#mytextbox").datepicker({ defaultDate: myDate });
spaceman