Hasif Subair
Hasif Subair

Reputation: 436

How to set date on jquery datepicker with java.util.Calendar class

I trying to edit an users profile. I have a "Date of birth" textfield, for which I use jqueryui date picker. At the server side I have struts2. Struts2 returns the date in java.util.Calendar object. I looking to find a way in which the default date of the datepicker can be set using this Calendar object.

Can this be done using struts-jquery plugin?

I tried like this

    $(function() {
        $("#txtDob").datepicker( "setDate" , <s:property value="#vendor.dateOfBirth" />);
        $( "#txtDob" ).datepicker({
            yearRange: '1930:-18',
            changeMonth: true,
            changeYear: true,
        });
    });

Nothing is happening, even the datepicker isn't working.

Any help will be appreciated.

Upvotes: 0

Views: 3104

Answers (2)

Johannes
Johannes

Reputation: 2070

With Struts2 jQuery Plugin you can use the value attribute from the DatePickerTag to set the value of your DatePicker.

<sj:datepicker value="%{vendor.dateOfBirth}" id="txtDob" name="dateOfBirth" label="Birthday" />

When using plain jQuery Ui and Struts2 you can use the Struts2 DateTag to set the Date.

$("#txtDob").val($.datepicker.formatDate(params.dateFormat, new Date(<s:date name="%{vendor.dateOfBirth}" format="yyyy, MM, dd" />)));

Note: You should init your datepicker before you set the Date via $(elem).datepicker( "setDate", val)

Upvotes: 1

Jubin Thomas
Jubin Thomas

Reputation: 1013

$("#yourinput").datepicker( "setDate" , "<% date_here %>" );

Upvotes: 1

Related Questions