javascript - how to get the selected year in datepicker? - Stack Overflow

I am using datepicker for my input name birth_day. And I want to calculate the age from the current yea

I am using datepicker for my input name birth_day. And I want to calculate the age from the current year and selected year.

current_yr - selected_yr

but I have no idea on how can I get the selected year from date picker.

this is my code look like.

<script>
    function dateYear(){
        var d = new Date();
                var year = d.getFullYear();
                return year;
    }
    $(function() {
        $( "#id_birth_date" ).datepicker({ dateFormat: 'yy-mm-dd' });

        //how can I get the selected year?
    });

</script>

<input type="text" name="birth_day" id="bd">
<input type="text" name="age" id="ag">

does anyone can give me an idea about my case?

any help will be appreciated ...

thanks in advance

I am using datepicker for my input name birth_day. And I want to calculate the age from the current year and selected year.

current_yr - selected_yr

but I have no idea on how can I get the selected year from date picker.

this is my code look like.

<script>
    function dateYear(){
        var d = new Date();
                var year = d.getFullYear();
                return year;
    }
    $(function() {
        $( "#id_birth_date" ).datepicker({ dateFormat: 'yy-mm-dd' });

        //how can I get the selected year?
    });

</script>

<input type="text" name="birth_day" id="bd">
<input type="text" name="age" id="ag">

does anyone can give me an idea about my case?

any help will be appreciated ...

thanks in advance

Share Improve this question asked Jun 26, 2012 at 3:43 gadssgadss 22.5k42 gold badges108 silver badges158 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

Do you mean:

$(function() {
     $("#id_birth_date").datepicker({
           onSelect: function(dateText, inst) {
              var startDate = new Date(dateText);
              var selectedYear = startDate.getFullYear(); // selected year
           }
     });
});

It will be possible.. after selecting date from datepicker

You will get property of datepicker1.year which will help you get year

Or

After retrieving date into string use Convert.Toint32(strDate1)-Convert.Toint32(strDate2) ;

..Try it...Otherwise post you code,...

You should be able to utilize the val() function.

The selected answer has a problem: the dateText may not be parsed because it depends on text which can be customised. Below is the enhanced accurate answer. Otherwise you will end up using an undefined date object:

$(function() {
     $("#id_birth_date").datepicker({
           onSelect: function(dateText, inst) {
              var startDate = new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay);
              var selectedYear = startDate.getFullYear(); // selected year
           }
     });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745360434a4624333.html

相关推荐

  • javascript - how to get the selected year in datepicker? - Stack Overflow

    I am using datepicker for my input name birth_day. And I want to calculate the age from the current yea

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信