【问题标题】:how to get the selected year in datepicker?如何在日期选择器中获取选定的年份?
【发布时间】:2012-06-26 03:43:00
【问题描述】:

我正在使用 datepicker 作为我的输入名称 birth_day。我想从当前年份和选定年份计算年龄。

current_yr - selected_yr

但我不知道如何从日期选择器中获取选定的年份。

这是我的代码样子。

<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">

有人可以告诉我我的情况吗?

任何帮助将不胜感激...

提前致谢

【问题讨论】:

    标签: javascript jquery jquery-ui datepicker


    【解决方案1】:

    你的意思是:

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

    【讨论】:

      【解决方案2】:

      有可能..从日期选择器中选择日期后

      您将获得 datepicker1.year 的属性,这将帮助您获得年份

      或者

      将日期检索为字符串后,使用 Convert.Toint32(strDate1)-Convert.Toint32(strDate2) ;

      ..尝试一下...否则发布您的代码,...

      【讨论】:

        【解决方案3】:

        您应该能够使用val() 功能。

        【讨论】:

          【解决方案4】:

          所选答案有问题:dateText 可能无法解析,因为它依赖于可以自定义的文本。以下是增强的准确答案。否则你最终会使用一个未定义的日期对象:

          $(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
                     }
               });
          });
          

          【讨论】:

            猜你喜欢
            • 2021-10-29
            • 2013-04-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-07-22
            • 2023-04-08
            相关资源
            最近更新 更多