Nupur
Nupur

Reputation: 41

How to calculate the age after selecting a date of birth in Struts 2?

I followed this link below to calculate the age based on the date of birth selection:

Getting age automatically when given Date of Birth

But I am not able to calculate the date in the age field.

I have used struts2-jquery-plugin-3.7.1.jar.

<sj:datepicker id="dob" name="dateOfBirth" label="Change Month and Year" changeMonth="true" changeYear="true" displayFormat ="dd-mm-yy"></sj:datepicker>    

<s:textfield class="w3-input w3-border" placeholder="Age" name="age" id="age"/> 

and the script which I used is:

<script>
    $('#dob').datepicker({
        onSelect: function(value, ui) {
            var today = new Date(),
                dob = new Date(value),
                age = new Date(today - dob).getFullYear() - 1970;

            $('#age').text(age);
        },
        maxDate: '+0d',
        yearRange: '1920:2010',
        changeMonth: true,
        changeYear: true
    });
</script>

Even I am not able to scroll through a list of years available, I am only getting a 10 years list (I want a scrollable year from 1990 to till 2016 that is the current year)

Where I am getting wrong? I am stuck with this, and I don't know the other solution to calculate the age!

Upvotes: -1

Views: 365

Answers (1)

Roman C
Roman C

Reputation: 1

The struts2-jquery plugin is using tags to render its own datepicker based on jquery ui added via plugin. You are messing a datetepicker bound to the same element. You should use either plugins' datepicker or native datepicker but not both.

To specify years range from 1990 till 2016 you can use option yearRange: '1900:2016'. This is the reference from jQuery API.

If you want to use plugins' version of datepicker then you should refer Date Picker Tag Wiki Page.

JSFiddle...

Upvotes: 0

Related Questions